July 11, 2013
Meta:

> That is a bit better, yes. Still somewhat clunky, but workable.

Recently Walter has proposed a small improvement able to reduce the size of that template. I don't know where Walter proposal has gone in the meantime, as people have suggested a better and more general design.


> It'd still be nice if it wasn't necessary.

Algebraic is still primitive, but improving both D and Phobos it can become better. It requires some discussions, design, and implementation work.

Bye,
bearophile
July 11, 2013
On Thursday, 11 July 2013 at 12:05:49 UTC, Meta wrote:
> On Thursday, 11 July 2013 at 04:03:19 UTC, Jesse Phillips wrote:
>> On Thursday, 11 July 2013 at 03:06:39 UTC, Meta wrote:
>>> struct Option(T)
>>> {	
>>>   Algebraic!(Some!T, None) payload;
>>>
>>>   alias payload this;
>>> }
>>
>> This is untested but it probably looks something like this:
>>
>>
>> private alias MaybeType = Algebraic!(Some!T, None);
>>
>> Option!int ans;
>> ans.payload = MaybeType(None);
>
> Ideally, payload would be private, and only exposed through alias this. It's somewhat unfortunate that this is necessary in the first place. D doesn't happen to have something like an opImplicitCast, does it?

opImplicitCast was rejected. As for private payload, this should also work.

auto ans = Option!int(MaybeType(None()));

I forgot to mention that most of the casting you were doing doesn't do what you think it would. If your types provided an opCast to the type you were requesting then that would have been used.

struct Maybe(T) {
    Option!T payload;
    alias payload this;
}

This doesn't make an Option!int castable to a Maybe!int, they are still distinct types:

struct Fish {
   int speed;
}

struct Dog {
   string name;
}

auto ans = cast(Fish) Dog();

And... compiler barf.
July 11, 2013
Jesse Phillips:

> I forgot to mention that most of the casting you were doing doesn't do what you think it would.

That's why I have suggested the OP to avoid to use cast() unless he/she knows well what she/he is doing.

Bye,
bearophile
July 11, 2013
On Thursday, 11 July 2013 at 18:31:50 UTC, Jesse Phillips wrote:
> I forgot to mention that most of the casting you were doing doesn't do what you think it would.
> ...
>
> This doesn't make an Option!int castable to a Maybe!int, they are still distinct types:
> ...

struct Test1
{
	int n;
}

struct Test2
{
	string s;
	Test1 t;
	
	alias t this;
}

void main()
{
	auto t1 = Test1();
	auto t2 = cast(Test2)t1; //Error
}

It seems you're right. That's disappointing. TDPL talks about alias this in terms of subtyping, but doesn't the above code show that this is not true subtyping?
July 11, 2013
On Thursday, 11 July 2013 at 18:49:27 UTC, Meta wrote:
> It seems you're right. That's disappointing. TDPL talks about alias this in terms of subtyping, but doesn't the above code show that this is not true subtyping?

Actually, scratch that, I'm an idiot. I was thinking completely backwards.
1 2
Next ›   Last »