The new ?? and ??? operators

Alexander Panek a.panek at brainsware.org
Mon Sep 24 23:30:35 PDT 2007


Arlen Albert Keshabyan wrote:
> It would be the sugar syntactic to add '??' operator to D. Consider the example code:
> 
> string error_message = getErrorMessage() ?? "no errors";
> A a = x.getPreparedAObject() ?? y.getPreparedAObject() ?? new A();
> 
> This operator is supposed to do the same thing as C# does: lvalue evaluates to the first non-null value or to null if all rvalues contains null. This operator might be extended to '???' to evaluate to a value that conforms to some conditions. The lvalue gets the first rvalue that evaluates to true (evaluation goes from left to right). If no conditions evaluates to true then the lvalue stays unchanged. If no conditions are given explicitly then those conditions evaluates to true (so, the best place for them is at the end of a sequence).
> 
> For instance:
> 
> int codeval = getValue1() > 5 ??? getValue2() >= 4 ??? getValue3() != 0 ??? 1; // if no conditions are satisfied then codeval = 1 as a default value at last. There are no sane reasons to place it in the middle of the sequence.
> 
> int code = 7;
> code = 5 < 10 ??? 0 != 0; // this way, code = 7 (no conditions are satisfied)
> 

if (auto result_of_complex_expression = 
complex_expression_or_function()) var = result_of_complex_expression;
// else var = null;



More information about the Digitalmars-d mailing list