www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - `alias this` pointers and typeof(null)

reply Tomer Filiba <tomerfiliba gmail.com> writes:
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
Jan 29
next sibling parent reply Andrea Fontana <nospam example.com> writes:
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?

 -tomer
What about this: http://dpaste.dzfl.pl/3305cdf8b7b4 ? Andrea
Jan 29
parent Tomer Filiba <tomerfiliba gmail.com> writes:
On Friday, 29 January 2016 at 13:59:15 UTC, Andrea Fontana wrote:
 What about this:
 http://dpaste.dzfl.pl/3305cdf8b7b4
 ?

 Andrea
Thanks Andrea, I thought about it but it requires duplicating all function signatures (and not in an automatic manner). Btw, you can also write ``void func(typeof(null) x) {}`` instead.
Jan 29
prev sibling parent reply Benjamin Thaut <code benjamin-thaut.de> writes:
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?

 -tomer
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. Kind Regards Benjamin Thaut
Jan 29
parent reply Nick Treleaven <ntrel-pub mybtinternet.com> writes:
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.
Jan 30
parent Tomer Filiba <tomerfiliba gmail.com> writes:
On Saturday, 30 January 2016 at 17:49:41 UTC, Nick Treleaven 
wrote:
 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.
+1 on that!
Jan 30