Adding the ?. null verification

Bienlein via Digitalmars-d digitalmars-d at puremagic.com
Fri Jun 20 07:07:06 PDT 2014


On Wednesday, 18 June 2014 at 15:57:40 UTC, Etienne wrote:
> On 2014-06-18 11:55 AM, bearophile wrote:
>> Etienne:
>>
>>> writeln(obj.member?.nested?.val);
>>
>> What about an approach like Scala instead?
>>
>> Bye,
>> bearophile
>
> You mean like this?
>
>
> http://stackoverflow.com/questions/1163393/best-scala-imitation-of-groovys-safe-dereference-operator
>
> def ?[A](block: => A) =
>   try { block } catch {
>     case e: NullPointerException if 
> e.getStackTrace()(2).getMethodName == "$qmark" => null
>     case e => throw e
>   }
>
> val a = ?(b.c.d.e)

I think he means to use the Option class instead of returning 
null. Also Rust does it that way.


More information about the Digitalmars-d mailing list