Non-null objects, the Null Object pattern, and T.init

Adam D. Ruppe destructionator at gmail.com
Thu Jan 16 18:11:21 PST 2014


On Friday, 17 January 2014 at 02:04:27 UTC, Andrei Alexandrescu 
wrote:
> Yah, that would be expected.

Yeah, but I think people would find it weird. This kind of thing 
is actually possible today:

class Foo { }

class Bar {
         Foo foo = new Foo(); // this gets a static reference into 
the typeinfo.init (since new Foo is evaluated at compile time!) 
which is blitted over...
}

void main() {
         auto bar = new Bar(); // bar.foo is blitted to point at 
the static Foo
         auto bar2 = new Bar(); // and same thing
         assert(bar.foo is bar2.foo); // passes, but that's kinda 
weird
}


Granted, maybe the weirdness here is because the variable isn't 
static, so people expect it to be different, but I saw at least 
one person on the chat room be very surprised by this - and I was 
too, until I thought about how CTFE and Classinfo.init is 
implemented, then it made sense, but at first glance it was a bit 
weird.

I think people would be a bit surprised if it was "Foo foo;" as 
well using the proposed .init thingy.


More information about the Digitalmars-d mailing list