www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Implicit conversion from null in custom type

reply "Vladimir Panteleev" <thecybershadow.lists gmail.com> writes:
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
next sibling parent reply "Kagamin" <spam here.lot> writes:
IIRC the rationale to ignore implicit conversions was to simplify 
function overloading rules.
May 28 2015
parent "Vladimir Panteleev" <thecybershadow gmail.com> writes:
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
prev sibling next sibling parent reply "Andrea Fontana" <nospam example.com> writes:
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
parent reply "Vladimir Panteleev" <thecybershadow gmail.com> writes:
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
parent reply "Andrea Fontana" <nospam example.com> writes:
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
next sibling parent "Adam D. Ruppe" <destructionator gmail.com> writes:
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
prev sibling parent "Vladimir Panteleev" <thecybershadow gmail.com> writes:
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
prev sibling parent reply "Meta" <jared771 gmail.com> writes:
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
parent reply "Vladimir Panteleev" <thecybershadow gmail.com> writes:
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
parent "Meta" <jared771 gmail.com> writes:
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:
 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.
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.
May 28 2015