Adding the ?. null verification
Frustrated via Digitalmars-d
digitalmars-d at puremagic.com
Wed Jun 18 10:31:37 PDT 2014
On Wednesday, 18 June 2014 at 15:42:04 UTC, Etienne wrote:
> I find myself often repeating this boilerplate:
>
> if (obj.member !is null)
> {
> if (obj.member.nested !is null)
> {
> if (obj.member.nested.val !is null)
> {
> writeln(obj.member.nested.val);
> }
> }
> }
>
> I have to admit it's a little frustrating when you see swift's
> ?. syntax notation, it would be a little more practical to be
> able to write
>
> writeln(obj.member?.nested?.val);
>
>
> Based on
> http://appventure.me/2014/06/13/swift-optionals-made-simple/
>
>
> Any thoughts?
Would be great! I think though, one could simplify it, you don't
need multiple ?'s(one would do)
if (?obj.member.nested.val == null)
{
...
}
?object returns null if the object is null, even if it is
dereferenced.
possibly a better syntax would be
if (??obj.member.nested.val == null)
If used as a rval one could have
auto x = ??obj.member.nested.val;
x is null if obj, obj.member, or obj.member.nested is null. Else
obj.member.nested.val.
I'm just curious who is going to add the the code to D?
If you don't like ?? (confusing it with the null-coalescing
operator) then what about ?*, *?, ?&, &?, ???, etc...
More information about the Digitalmars-d
mailing list