digitalmars.D.learn - Implicit conversion from null in custom type
- Vladimir Panteleev (8/8) May 28 2015 I'm trying to write a type which (to some extent) emulates
- Kagamin (2/2) May 28 2015 IIRC the rationale to ignore implicit conversions was to simplify
- Vladimir Panteleev (2/4) May 28 2015 Isn't this a requirement for making AAs user types anyway?
- Andrea Fontana (4/12) May 28 2015 What's the problem with ctor taking typeof(null)?
- Vladimir Panteleev (12/14) May 28 2015 It doesn't work:
- Andrea Fontana (4/18) May 28 2015 void fun(typeof(null)) { }
- Adam D. Ruppe (10/11) May 28 2015 You don't have to do that for built-in arrays though.
- Vladimir Panteleev (3/5) May 28 2015 That doesn't help with creating a drop-in replacement for an AA
- Meta (21/29) May 28 2015 What about defining a static `nil` value for S?
- Vladimir Panteleev (3/4) May 28 2015 Might as well just use S.init. Again, doesn't help with creating
- Meta (6/10) May 28 2015 Yeah, it's more or less the same as init, except you have control
I'm trying to write a type which (to some extent) emulates built-in AAs. One thing I'm having trouble with is null function parameters: void fun(S s) {} void main() { fun(null); } How can S implement implicit conversion from null? I've already tried "alias this" and a constructor taking typeof(null).
May 28 2015
IIRC the rationale to ignore implicit conversions was to simplify function overloading rules.
May 28 2015
On Thursday, 28 May 2015 at 11:37:34 UTC, Kagamin wrote:IIRC the rationale to ignore implicit conversions was to simplify function overloading rules.Isn't this a requirement for making AAs user types anyway?
May 28 2015
What's the problem with ctor taking typeof(null)? I've just used it, maybe I missed something? On Thursday, 28 May 2015 at 11:19:39 UTC, Vladimir Panteleev wrote:I'm trying to write a type which (to some extent) emulates built-in AAs. One thing I'm having trouble with is null function parameters: void fun(S s) {} void main() { fun(null); } How can S implement implicit conversion from null? I've already tried "alias this" and a constructor taking typeof(null).
May 28 2015
On Thursday, 28 May 2015 at 12:37:52 UTC, Andrea Fontana wrote:What's the problem with ctor taking typeof(null)? I've just used it, maybe I missed something?It doesn't work: ////////// test.d ///////// struct S { this(typeof(null) p) {} } void fun(S s) {} void main() { fun(null); } /////////////////////////// test.d(7): Error: function test.fun (S s) is not callable using argument types (typeof(null))
May 28 2015
void fun(typeof(null)) { } ? On Thursday, 28 May 2015 at 13:06:27 UTC, Vladimir Panteleev wrote:On Thursday, 28 May 2015 at 12:37:52 UTC, Andrea Fontana wrote:What's the problem with ctor taking typeof(null)? I've just used it, maybe I missed something?It doesn't work: ////////// test.d ///////// struct S { this(typeof(null) p) {} } void fun(S s) {} void main() { fun(null); } /////////////////////////// test.d(7): Error: function test.fun (S s) is not callable using argument types (typeof(null))
May 28 2015
On Thursday, 28 May 2015 at 13:12:18 UTC, Andrea Fontana wrote:void fun(typeof(null)) { }You don't have to do that for built-in arrays though. BTW I literally just wrote a slide on this for my Friday talk before reading this thread. I think D could have some implicit struct constructors, like C++, I just think C++ got it wrong by making implicit the default. That said, I don't think this is a hugely important language feature, and is one I would recommend against using very often, but in select places like this - such as emulating built-in arrays perfectly - it would be nice to have.
May 28 2015
On Thursday, 28 May 2015 at 13:12:18 UTC, Andrea Fontana wrote:void fun(typeof(null)) { } ?That doesn't help with creating a drop-in replacement for an AA (or any built-in type implicitly convertible from null).
May 28 2015
On Thursday, 28 May 2015 at 11:19:39 UTC, Vladimir Panteleev wrote:I'm trying to write a type which (to some extent) emulates built-in AAs. One thing I'm having trouble with is null function parameters: void fun(S s) {} void main() { fun(null); } How can S implement implicit conversion from null? I've already tried "alias this" and a constructor taking typeof(null).What about defining a static `nil` value for S? import std.stdio; struct S { static S nil = S(0); int n; } void fun(S s) { if (s == S.nil) writeln("null S"); else writeln("Non-null S"); } void main() { fun(S.nil); }
May 28 2015
On Thursday, 28 May 2015 at 13:46:52 UTC, Meta wrote:What about defining a static `nil` value for S?Might as well just use S.init. Again, doesn't help with creating a drop-in replacement.
May 28 2015
On Thursday, 28 May 2015 at 13:52:03 UTC, Vladimir Panteleev wrote:On Thursday, 28 May 2015 at 13:46:52 UTC, Meta wrote:Yeah, it's more or less the same as init, except you have control over it and the struct doesn't default to it. We really do need some kind of implicit conversion facility when passing to and returning from functions, though.What about defining a static `nil` value for S?Might as well just use S.init. Again, doesn't help with creating a drop-in replacement.
May 28 2015