Difficulties with std.variant.Algebraic

Jesse Phillips Jesse.K.Phillips+D at gmail.com
Thu Jul 11 11:31:49 PDT 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.


More information about the Digitalmars-d-learn mailing list