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

Artur Skawina art.08.09 at gmail.com
Fri Jan 17 04:18:32 PST 2014


On 01/17/14 06:33, Walter Bright wrote:
> 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() { ... }


   default() { ... }

because

  a) you've just reinvented default construction;
  b) overloading 'init' like that is a bad idea (should this
     'init()' be called before invoking a "normal" ctor?...)

artur



More information about the Digitalmars-d mailing list