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

Walter Bright newshound2 at digitalmars.com
Thu Jan 16 21:33:38 PST 2014


On 1/16/2014 5:42 PM, Andrei Alexandrescu wrote:
> Walter and I were talking today about the null pointer issue and he had the
> following idea.
>
> One common idiom to replace null pointer exceptions with milder reproducible
> errors is the null object pattern, i.e. there is one object that is used in lieu
> of the null reference to initialize all otherwise uninitialized references. In D
> that would translate naturally to:
>
> class Widget
> {
>      private int x;
>      private Widget parent;
>      this(int y) { x = y; }
>      ...
>      // Here's the interesting part
>      static Widget init = new Widget(42);
> }

I was thinking of:

       @property static Widget init() { ... }



More information about the Digitalmars-d mailing list