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
public class ok {
T s;
public static implicit operator ok(T s) { return new ok { s = s }; }
public static implicit operator T(ok _) { return _.s; }
public static bool operator true(ok _) { return _.s != null; }
public static bool operator false(ok _) { return _.s == null; }
public static ok operator &(ok x, ok y) { return y; }
}
I often need this logic for strings:
using ok = ok;
...
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"