digitalmars.D - `alias this` pointers and typeof(null)
- Tomer Filiba (24/24) Jan 29 2016 I had a struct (Foo) that was passed by pointer (i.e., Foo*)
- Andrea Fontana (5/12) Jan 29 2016 What about this:
- Tomer Filiba (4/8) Jan 29 2016 Thanks Andrea, I thought about it but it requires duplicating all
- Benjamin Thaut (7/14) Jan 29 2016 Unfortunately D is strictly against implict conversion, so there
- Nick Treleaven (9/22) Jan 30 2016 I've heard (on This Week in D) that C++ implicit construction
- Tomer Filiba (3/11) Jan 30 2016 +1 on that!
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 2016
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? -tomerWhat about this: http://dpaste.dzfl.pl/3305cdf8b7b4 ? Andrea
Jan 29 2016
On Friday, 29 January 2016 at 13:59:15 UTC, Andrea Fontana wrote:What about this: http://dpaste.dzfl.pl/3305cdf8b7b4 ? AndreaThanks 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 2016
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? -tomerUnfortunately 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 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'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.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.
Jan 30 2016
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 2016