`alias this` pointers and typeof(null)

Nick Treleaven via Digitalmars-d digitalmars-d at puremagic.com
Sat Jan 30 09:49:41 PST 2016


On Friday, 29 January 2016 at 15:38:26 UTC, Benjamin Thaut wrote:
> On Friday, 29 January 2016 at 13:38:20 UTC, Tomer Filiba wrote:
>>
>> I can change all such invocations into ``func(FooPtr(null))`` 
>> but it's tedious and basically requires me to compile tens of 
>> times before I'd cover everything. Is there some workaround to 
>> make null implicitly convertible to my alias-this type? I 
>> mean, it's Foo* would accept `typeof(null)` so why can't 
>> FooPtr with an alias-this to Foo* do so too?
>>
> Unfortunately D is strictly against implict conversion, so 
> there is no way to do this. I also hit this issue a couple of 
> times already. But implicitly calling a constructor in C++ is 
> considered error prone and therefor not supported in D.

I've heard (on This Week in D) that C++ implicit construction 
wouldn't be so bad if it only worked when a constructor was 
specially marked for that purpose, e.g.:

struct FooPtr {
   ...
   @implicit this(typeof(null)) {ptr = null;}
}

Then null could be passed as a FooPtr.



More information about the Digitalmars-d mailing list