`alias this` pointers and typeof(null)

Tomer Filiba via Digitalmars-d digitalmars-d at puremagic.com
Fri Jan 29 05:38:20 PST 2016


I had a struct (Foo) that was passed by pointer (i.e., Foo*) 
throughout my code. To prevent dangling pointers, I created a 
`FooPtr` struct with an `alias this` that does some checks before 
returning me the underlying pointer. This is sort-of what I have:

struct FooPtr {
     Foo* ptr;

     this(Foo* p) {ptr = p;}
     @property auto get() {return ptr;}
     alias get this;
}

I changed all `Foo*` into `FooPtr` and it's great, but I have 
numerous places where `null` is used literally, and its type is 
`typeof(null)`. So

void func(FooPtr x) {}
func(null);

fails with ``Error: function func (FooPtr x) is not callable 
using argument types (typeof(null))``

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?

-tomer


More information about the Digitalmars-d mailing list