Is there an “opposite” to the null coalescing operator? (…in any language?)

后端 未结 12 1054
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-04 18:38

null coalescing translates roughly to return x, unless it is null, in which case return y

I often need return null if x is null, otherwise return x

相关标签:
12条回答
  • 2020-12-04 19:22

    It just felt right to add this as an answer.

    I guess the reason why there is no such thing in C# is because, unlike the coalescing operator (which is only valid for reference types), the reverse operation could yield either a reference or value type (i.e. class x with member int y - therefore it would unfortunately be unusable in many situations.

    I'm not saying, however, that I wouldn't like to see it!

    A potential solution to that problem would for the operator to automatically lift a value type expression on the right-hand-side to a nullable. But then you have the issue that x.y where y is an int will actually return an int? which would be a pain.

    Another, probably better, solution would be for the operator to return the default value (i.e. null or zero) for the type on the right hand side if the expression on the left is null. But then you have issues distinguishing scenarios where a zero/null was actually read from x.y or whether it was supplied by the safe-access operator.

    0 讨论(0)
  • 2020-12-04 19:26

    There's the null-safe dereferencing operator (?.) in Groovy... I think that's what you're after.

    (It's also called the safe navigation operator.)

    For example:

    homePostcode = person?.homeAddress?.postcode
    

    This will give null if person, person.homeAddress or person.homeAddress.postcode is null.

    (This is now available in C# 6.0 but not in earlier versions)

    0 讨论(0)
  • 2020-12-04 19:30

    We considered adding ?. to C# 4. It didn't make the cut; it's a "nice to have" feature, not a "gotta have" feature. We'll consider it again for hypothetical future versions of the language, but I wouldn't hold my breath waiting if I were you. It's not likely to get any more crucial as time goes on. :-)

    0 讨论(0)
  • 2020-12-04 19:30

    Create a static instance of your class somewhere with all the right default values for the members.

    For example:

    z = new Thingy { y=null };

    then instead of your

    return x != null ? x.y : null;

    you can write

    return (x ?? z).y;
    0 讨论(0)
  • 2020-12-04 19:32

    In Haskell, you can use the >> operator:

    • Nothing >> Nothing is Nothing
    • Nothing >> Just 1 is Nothing
    • Just 2 >> Nothing is Nothing
    • Just 2 >> Just 1 is Just 1
    0 讨论(0)
  • 2020-12-04 19:34
    public class ok<T> {
        T s;
        public static implicit operator ok<T>(T s) { return new ok<T> { s = s }; }
        public static implicit operator T(ok<T> _) { return _.s; }
    
        public static bool operator true(ok<T> _) { return _.s != null; }
        public static bool operator false(ok<T> _) { return _.s == null; }
        public static ok<T> operator &(ok<T> x, ok<T> y) { return y; }
    }
    

    I often need this logic for strings:

    using ok = ok<string>;
    
    ...
    
    string bob = null;
    string joe = "joe";
    
    string name = (ok)bob && bob.ToUpper();   // name == null, no error thrown
    string boss = (ok)joe && joe.ToUpper();   // boss == "JOE"
    
    0 讨论(0)
提交回复
热议问题