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

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Thu Jan 16 18:04:26 PST 2014


On 1/16/14 5:53 PM, Adam D. Ruppe wrote:
> On Friday, 17 January 2014 at 01:42:38 UTC, Andrei Alexandrescu wrote:
>>     static Widget init = new Widget(42);
>
> The problem here is if it is in a mutable data segment:
>
> Widget i; // implicitly set to init
> assert(i is Widget.init); // uh oh
> i.mutate();
>
> Widget i2; // set to widget init...
> assert(i is i2); // oh dear
> // i2 now reflects the mutation done above!

Yah, that would be expected.

> I'm not necessarily against having the option, but I think it would
> generally be more trouble than it is worth. If you have an object that
> you want to guarantee is not null, make the nullable implementation
>
> class Widget_impl {}
>
> then define
>
> alias Widget = NotNull!Widget_impl;
>
> and force initialization that way.

Noted.


Andrei



More information about the Digitalmars-d mailing list