The new ?? and ??? operators

Max Samukha samukha at voliacable.com.removethis
Tue Sep 25 22:55:51 PDT 2007


On Tue, 25 Sep 2007 22:33:15 +1000, Reiner Pope <some at address.com>
wrote:

>Looks good. And we can overload the bitwise OR operator to make it look 
>a little better (in my opinion). We just add
>
>         Nullable!(T) opOr(Nullable!(T) other)
>         {
>             return isNull ? other : Type(value);
>         }
>
>         T opOr(T other)
>         {
>             return isNull ? other : value;
>         }
>
>to your struct, and then as a new main, we get
>
>void main()
>{
>     auto data = new DataReader;
>
>     auto id = data.get!(long)("id");
>     assert(id == null);
>
>     auto id2 = data.get!(long)("id2");
>     id2 = 25;
>
>     auto y = id | id2; // y is a Nullable!(T)
>     assert(y != null);
>
>     long x = y | -1;
>     assert (x == 25);
>
>     id = 20;
>     assert(id | id2 | -1 == 20);
>}
>
>
>Voila!
>
>Unfortunately, I couldn't get short-circuiting to work via using lazy on 
>parameters types to opOr. This is because | is parsed 
>left-associatively, so that doesn't work. I wonder if some 
>expression-templatey work could fix this...
>
>     -- Reiner

I like the way it looks but changing the meaning of overloaded
operators is not recommended by the dogmata. It seems like ?? operator
would be a useful addition to the language.



More information about the Digitalmars-d mailing list