Adding the ?. null verification
H. S. Teoh via Digitalmars-d
digitalmars-d at puremagic.com
Thu Jun 19 13:51:17 PDT 2014
On Thu, Jun 19, 2014 at 04:29:12PM -0400, Etienne via Digitalmars-d wrote:
> On 2014-06-19 3:11 PM, H. S. Teoh via Digitalmars-d wrote:
> >
> > safeDeref(obj).subobj.prop.field.failsafe!val(defaultVal)
> >
> >where the last element is passed as an alias to the failsafe template,
> >which then does the .init-replacement magic. It looks a lot uglier,
> >though. :-(
> >
> >
> >T
> >
>
> meh, this works:
>
>
> writeln(currAssignment.safeDeref.typeInfo.ident.or("meh"));
>
> ..
> static struct SafeDeref {
> T t;
>
> // Make the wrapper as transparent as possible.
> alias t this;
>
> // this overrides if null
> T or(T defVal){
> if (t is t.init)
This assumes that t.init is not a possible valid field value. But in
that case, there's no need to remap it, you just check for t.init
instead. For pointers, where .init is null, this isn't a problem, but
for things like int, where 0 is possible valid value, you may be
accidentally mapping 0 to the default value when the given field
actually exists (and has value 0)!
> {
> return defVal;
> }
> else
> {
> return t;
> }
>
> }
> ..
T
--
Gone Chopin. Bach in a minuet.
More information about the Digitalmars-d
mailing list