Thread overview | |||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
May 28, 2015 Implicit conversion from null in custom type | ||||
---|---|---|---|---|
| ||||
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 Re: Implicit conversion from null in custom type | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vladimir Panteleev | IIRC the rationale to ignore implicit conversions was to simplify function overloading rules. |
May 28, 2015 Re: Implicit conversion from null in custom type | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vladimir Panteleev | 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 Re: Implicit conversion from null in custom type | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrea Fontana | 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 Re: Implicit conversion from null in custom type | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kagamin | 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 Re: Implicit conversion from null in custom type | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vladimir Panteleev | 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 Re: Implicit conversion from null in custom type | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrea Fontana | 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 Re: Implicit conversion from null in custom type | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrea Fontana | 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 Re: Implicit conversion from null in custom type | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vladimir Panteleev | 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 Re: Implicit conversion from null in custom type | ||||
---|---|---|---|---|
| ||||
Posted in reply to Meta | 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.
|
Copyright © 1999-2021 by the D Language Foundation