Null-Coalescing Operator and Extensions

aliak something at something.com
Tue Aug 28 18:53:37 UTC 2018


On Tuesday, 28 August 2018 at 13:27:28 UTC, Simen Kjærås wrote:
> Now, as has been pointed out, that only work for 
> null-coalescing, not null-propagation. It seems writers of 
> Optional, Variant, SumType, and so on, have decided not to 
> support this out of the box, but rather wrap it separately, 
> like Basile B.'s SafeAccess[1] and aliak's dispatch[2]. There's 
> no real obstacle to wrapping member access directly in 
> Optional!T such that it always return a 
> Optional!(typeof(member)), though.

I've been back and forth on this many times with an Optional 
type. I've come to the conclusion that providing a "safe" 
opDispatch in the optional type itself is worth if it the 
Optional type does not have any member functions. Otherwise it'll 
essentially cripple the user type if it has functions named the 
same. And since the optional types (presented around the forums) 
seem to support range access, it basically means you can't have 
an Optional!Range type - or any T that has a member called empty 
(which is not uncommon IMO). I can't figure out a way around this 
:(

Cheers,
- Ali


More information about the Digitalmars-d-learn mailing list