Adding the ?. operator to D

Jacob Carlborg doob at me.com
Wed Sep 16 19:01:28 UTC 2020


On 2020-09-12 18:51, Per Nordlöw wrote:
> Has there been any discussions on adding the ?. operator to simplify 
> structure matching on class trees typically found in compilers such as dmd.
> 
> Basically, implemented via lowering of
> 
>      C ?. EXPR
> 
> to
> 
>      C ? C.EXPR : EXPR.init
> 
> when EXPR has type class or pointer and for the other cases either
> 
>      C ? C.EXPR : EXPR.init
> 
> or perhaps even
> 
>      C ? C.EXPR : nullable(EXPR).init
> 
> if nullable is made part of the runtime or even builtin.
> 
> Given that `C` is a class or pointer to an aggregate.

I would like that. It should work for an Optional/Nullable type as well. 
It's a good idea to add an `??` operator at the same time as well:

auto a = A ?? B;

if `A` is not null, assign `A` to `a`. Otherwise assign `B` to `a`.

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list