www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - std.Algebraic alias this

reply Radu <radu void.null> writes:
There is a weird rule on how compiler treats alias this for the N 
and S types bellow.

As you can see, somehow S losses it's type and ends up a plain 
string withing the Algebraic type. I would expect that all types 
should be the treated the same, why a string alias will be 
different then a bool or a double?

int main(string[] argv)
{
	import std.variant	: Algebraic;

	struct N { double val; alias val this; }
	struct S { string val; alias val this; }
	alias T = Algebraic!(N, S);
	pragma(msg, T.AllowedTypes); // prints (N, string)??

	T t = N(1.0);
	assert(t.get!N == 1.0); // works
	// t = 0.4; // fails, OK, variant.d(586): Error: static assert  
"Cannot store a double in a VariantN!(8u, N, S). Valid types are 
(N, string)"
	// t = S("foo"); // this fails, why? Error: static assert  
"Cannot store a S in a VariantN!(8u, N, S). Valid types are (N, 
string)"
	t = "bar"; // this works... why?
	// assert(t.get!S == "bar"); //this fails Variant: attempting to 
use incompatible types immutable(char)[] and main.main.S at 
std\variant.d(1475)
	assert(t.get!string == "bar"); // works, why?
	
	return 0;
}
Oct 05 2015
parent reply Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Monday, 5 October 2015 at 11:31:32 UTC, Radu wrote:
 There is a weird rule on how compiler treats alias this for the 
 N and S types bellow.

 [...]
Please file a bug report. Also do the errors change if you reverse the order in T i.e. alias T = Algebraic!(S,N); ?
Oct 05 2015
next sibling parent Radu <radu void.null> writes:
On Tuesday, 6 October 2015 at 06:37:16 UTC, Nicholas Wilson wrote:
 On Monday, 5 October 2015 at 11:31:32 UTC, Radu wrote:
 There is a weird rule on how compiler treats alias this for 
 the N and S types bellow.

 [...]
Please file a bug report. Also do the errors change if you reverse the order in T i.e. alias T = Algebraic!(S,N); ?
OK, will do. Nope alias T = Algebraic!(S,N) gives the same error. AllowedTypes are (string, N) This happens in 2.068.2 btw
Oct 05 2015
prev sibling parent Radu <radu void.null> writes:
On Tuesday, 6 October 2015 at 06:37:16 UTC, Nicholas Wilson wrote:
 On Monday, 5 October 2015 at 11:31:32 UTC, Radu wrote:
 There is a weird rule on how compiler treats alias this for 
 the N and S types bellow.

 [...]
Please file a bug report. Also do the errors change if you reverse the order in T i.e. alias T = Algebraic!(S,N); ?
bug report https://issues.dlang.org/show_bug.cgi?id=15168
Oct 06 2015