Jump to page: 1 2
Thread overview
Implicit conversion from null in custom type
May 28, 2015
Vladimir Panteleev
May 28, 2015
Kagamin
May 28, 2015
Vladimir Panteleev
May 28, 2015
Andrea Fontana
May 28, 2015
Vladimir Panteleev
May 28, 2015
Andrea Fontana
May 28, 2015
Adam D. Ruppe
May 28, 2015
Vladimir Panteleev
May 28, 2015
Meta
May 28, 2015
Vladimir Panteleev
May 28, 2015
Meta
May 28, 2015
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
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
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
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.
« First   ‹ Prev
1 2