Re: Difficulties with std.variant.Algebraic

2013-07-11 Thread Meta
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.

Re: Difficulties with std.variant.Algebraic

2013-07-11 Thread Meta
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; } s

Re: Difficulties with std.variant.Algebraic

2013-07-11 Thread bearophile
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

Re: Difficulties with std.variant.Algebraic

2013-07-11 Thread Jesse Phillips
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 probabl

Re: Difficulties with std.variant.Algebraic

2013-07-11 Thread bearophile
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 s

Re: Difficulties with std.variant.Algebraic

2013-07-11 Thread Meta
On Thursday, 11 July 2013 at 17:11:05 UTC, Meta wrote: On Thursday, 11 July 2013 at 16:48:00 UTC, bearophile wrote: That is a bit better, yes. Still somewhat clunky, but workable. It'd still be nice if it wasn't necessary. Why does it break when I use Some!int instead of just a bare int? Oh,

Re: Difficulties with std.variant.Algebraic

2013-07-11 Thread Meta
On Thursday, 11 July 2013 at 16:48:00 UTC, bearophile wrote: Meta: Nullable will work in this case, but it doesn't solve the general problem with Algebraic. Then is this an acceptable solution? ... That is a bit better, yes. Still somewhat clunky, but workable. It'd still be nice if it was

Re: Difficulties with std.variant.Algebraic

2013-07-11 Thread bearophile
Meta: Nullable will work in this case, but it doesn't solve the general problem with Algebraic. Then is this an acceptable solution? import std.variant; struct None {} template Option(T) { alias Option = Algebraic!(T, None); } Option!int unreliable(int val) pure nothrow { if (val

Re: Difficulties with std.variant.Algebraic

2013-07-11 Thread Meta
On Thursday, 11 July 2013 at 12:30:17 UTC, bearophile wrote: Algebraic has several problems, but your code has other problems. Oh, no doubt. This isn't meant to be serious, industrial strength code. I suggest to take a look at Nullable, especially the version that makes a constant value the

Re: Difficulties with std.variant.Algebraic

2013-07-11 Thread bearophile
Nullable!(int, 0) unreliable2(in int val) pure nothrow { if (val == 0) { return typeof(return)(); } else { return typeof(return)(val); } } Sorry, I meant: import std.typecons; Nullable!int unreliable1(in int val) pure nothrow { if (val == 0) { return typ

Re: Difficulties with std.variant.Algebraic

2013-07-11 Thread bearophile
Some example code: import std.typecons; Nullable!int unreliable1(in int val) pure nothrow { if (val == 0) { return typeof(return)(); } else { return typeof(return)(val); } } Nullable!(int, 0) unreliable2(in int val) pure nothrow { if (val == 0) { return t

Re: Difficulties with std.variant.Algebraic

2013-07-11 Thread bearophile
Meta: I don't know whether to laugh or cry. Algebraic has several problems, but your code has other problems. I suggest to take a look at Nullable, especially the version that makes a constant value the "null". Also try to almost never use cast() in your code, unless you _really_ know wha

Re: Difficulties with std.variant.Algebraic

2013-07-11 Thread Meta
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

Re: Difficulties with std.variant.Algebraic

2013-07-10 Thread Jesse Phillips
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 = Mayb

Difficulties with std.variant.Algebraic

2013-07-10 Thread Meta
I've been playing around with std.variant.Algebraic, trying to define a very simple option type. However, I've been running into quite a few problems. My implementation is as follows: import std.conv; import std.variant; struct Some(T) { T payload; alias payload this; } struct None {