Adding the ?. null verification

Ary Borenszweig via Digitalmars-d digitalmars-d at puremagic.com
Wed Jun 18 09:21:54 PDT 2014


On 6/18/14, 1:17 PM, Etienne wrote:
>> I want non-null in the type system! To put a nullable reference into a
>> non-null variable you need to check first, but from then on the compiler
>> can ensure it's never null!
>
> That would be great but useless for my situation involving some complex
> AST's
>
> class AEntity
> {
>      Module head;
>      string ident;
>      Type type;
>      AEntity typeInfo;
>      AEntity[] members; // for definitions
>      AEntity[] parameters; // if parameterized
> ...
> }
>
> It's very useful to descend through parts of a complex tree without
> adding plenty of bool checks
>
> foreach (AEntity member; entity.members)
> {
>      if (member.typeInfo !is null)
>      {
>          AEntity curr = member;
>          member.ident ~= member.typeInfo.ident;
>          curr = member.typeInfo;
>          while (curr.typeInfo !is null)
>          {
>              member.ident ~= "." ~ curr.typeInfo.ident;
>              curr = curr.typeInfo;
>          }
>      }
> }

But if you don't add the bool checks you might get segmentation fault. 
Or am I missing something?



More information about the Digitalmars-d mailing list