I suggest you ...

Structure All Nullable Values

.NET Framework already has Nullable<T> but it could also have ToNullable<T> as a struct.

If you wanted to keep reference-types null and turn value-types to null you could make this:

class someGenericClass<T>
{
T? value; /* = Nullable<T> value; but this gives error
because T may already be null */

T$ value; /* = ToNullable<T> value; this doesn't give error,
what you wnat is keep what's null null,
and turn non-null into null */
}

The only problem is that this does not exist!
If you don't understand anything about this, contact jmcf125@gmail.com.

239 votes
Vote
Sign in
Check!
(thinking…)
Reset
or sign in with
  • facebook
  • google
    Password icon
    I agree to the terms of service
    Signed in as (Sign out)
    You have left! (?) (thinking…)
    JMCF125JMCF125 shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

    15 comments

    Sign in
    Check!
    (thinking…)
    Reset
    or sign in with
    • facebook
    • google
      Password icon
      I agree to the terms of service
      Signed in as (Sign out)
      Submitting...
      • Anonymous commented  ·   ·  Flag as inappropriate

        what i ran into before and i hated it, cause it was such a pain to come back to
        was casting nullables back to non-nullables

      • PhilippePhilippe commented  ·   ·  Flag as inappropriate

        The real problem is that generic are quite limited (compared to C++ template) when it come to metaprogramming. I think it should be improved as a whole. For example, it it where possible to overload generics on contraint like struct or class, then it would be possible to have something like:

        // Compiler would select this class if T is a reference type
        class A<T> where T : class
        {
        public T Value { get { return t; } set { t = value; } }
        public bool HasValue { get { return t != null; } }
        private T t;
        }

        // Compiler would select this class if T is a value type.
        class A<T> : Nullable<T> where T : struct
        {
        }

        //Not sure for the case where T is already a Nullable<T>
        class A<T> : T where T : Nullable<T>
        {
        }

        Same thing could apply to function.

        Compilation would follows same rule as for a single class concerning use across assemblies. In the case of functions, rules would be essentially the same as for overloaded functions.

      • KeithKeith commented  ·   ·  Flag as inappropriate

        Uh .. to me, this is one of those things that if you really, really need it .. you can write yourself. It looks like a 'edge case' kind of functional programming construct, that many will not have a lot of use for .. . Also, since everything in C#, all expressions anyway, evaluate to a type .. this sort (something like an 'option type' for example) sort of goes against that philosophy. Just my .02

      • JMCF125JMCF125 commented  ·   ·  Flag as inappropriate

        "you can't put reference values into a Nullable<T>". Yes, I knew that, yet my objective was to enable Nullable<T> to do so, by making changes to the CLR.
        I'm now using Nullable<Option<T>> value;.
        Thank you for your help!
        I have already posted this in http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/2cb7f451-bd56-480d-970b-ddb241889099.
        Should I remove this idea since it is now proved to be a question?

      • JMCF125JMCF125 commented  ·   ·  Flag as inappropriate

        I really need this.
        I've been stuck from progress for a long time.
        I'm yet trying to find this miraculous ToNullable<T> type.

      • JMCF125JMCF125 commented  ·   ·  Flag as inappropriate

        Thank you Mauricio.
        I want to use SASA butI can't find this ToNullable type. Is there any manual or something I could use to find it?

      • Mauricio SchefferMauricio Scheffer commented  ·   ·  Flag as inappropriate

        I think you're looking for an option type. It's very simple to implement but you can just get it from libraries like Sasa ( https://sourceforge.net/projects/sasa/ ), Functional-dotnet ( http://code.google.com/p/functional-dotnet/ ) or the F# runtime (FSharpx adds sugar for C#/VB.NET: https://github.com/fsharp/fsharpx )
        More importantly, option types are composable and simple to use and understand. They don't require any modifications to the language or the type system.

      • JMCF125JMCF125 commented  ·   ·  Flag as inappropriate

        That's what I wanted: "use Nullable<T> without regard for the type of T". Of course "HasValue" property would be changed too.

      • supercat57supercat57 commented  ·   ·  Flag as inappropriate

        What would break if Nullable<T> simply didn't care whether it was used redundantly? Unboxing a string to a Nullable<string> should copy the reference to the internal "value" field, and set "HasValue" according to whether the reference is non-null. Unboxing a string to a Nullable<Nullable<string>> could do likewise, but setting both "HasValue" fields appropriately. The "HasValue" field would be largely redundant, but a lot of generic code could be cleaner if one could simply use Nullable<T> without regard for the type of T.

      • David V. CorbinDavid V. Corbin commented  ·   ·  Flag as inappropriate

        I have used similar patterns, but they are not bulletproof, and to the best of my knowledge, can not be made 100% bullet proof AND have equivilant performance without CLR changes (or at least language changes to access CLR functionallity).

      • JMCF125JMCF125 commented  ·   ·  Flag as inappropriate

        I used the $ sign to show ToNullable<T> could be used as Nullable<T>, however another symbol, like § could be used.

        In my example, I wanted to make sure value is nullable, but I don't know if it alredy is. If it is, nothing changes, if it isn't, it is turned into nullable.

        Nullable<T> gives error, because the value may alredy be null, so I'd use ToNullable<T>, that makes sure the value is null. Therefore, I can assign the value to null.

      • QwertieQwertie commented  ·   ·  Flag as inappropriate

        This reminds me of the C++/CLI feature where you can access boxed structure members directly, without unboxing. It would be neat if you could somehow tell the compiler that you want to use a boxed value type without losing its type identity. I'm not quite sure why they invented Nullable<T> when the CLR has had the built-in ability to convert value types into (nullable) reference types since v1.0.

        I could think of better uses for a dollar sign, though. If they hadn't already used "?" for nullable types, it seems like it would have been ideal for what you are suggesting. Imagine if "int?" were still a nullable int (a boxed int if not null), and T? would be a reference type OR a boxed value type.

      Feedback and Knowledge Base