www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - T[#] can be a type or an array of types

reply BCS <ao pathlink.com> writes:
not exactly a bug but this is a little weird.


void Foo(alias Tfn)()
{
	Tfn!()[2] x = void;
	pragma(msg,typeof(x).stringof);
	x[0] = Tfn!().init;
}

template Bob() { alias T!(int, uint, char) Bob; }
template Joe() { alias int Joe; }


template T(t...){alias t T;}

alias Foo!(Bob) fig;
alias Foo!(Joe) fig;

void main(){}
Jun 03 2008
parent reply "Koroskin Denis" <2korden gmail.com> writes:
On Wed, 04 Jun 2008 05:49:55 +0400, BCS <ao pathlink.com> wrote:

 not exactly a bug but this is a little weird.


 void Foo(alias Tfn)()
 {
 	Tfn!()[2] x = void;
 	pragma(msg,typeof(x).stringof);
 	x[0] = Tfn!().init;
 }

 template Bob() { alias T!(int, uint, char) Bob; }
 template Joe() { alias int Joe; }


 template T(t...){alias t T;}

 alias Foo!(Bob) fig;
 alias Foo!(Joe) fig;

 void main(){}

Don't hesitate to post expected and actual output as well. Let's look at the simplified version of Foo, first: void Foo(alias Tfn)() { Tfn!() x; pragma(msg,typeof(x).stringof); } It produces the following ouput (two template instantiations): 1) (int, uint, char) // ValueTuple!(int, uint, char), see std.typecons for ValueTuple definition 2) int // int Reverting back we get the following output: 1) char // ValueTuple!(int, uint, char)[2] -> char 2) int[2u] // int[2] Did you expect to get a static array of Value tuples here instead of char? Well, there is some kind of an ambiguity, how do tread ValueTuple!(int, uint, char)[2]: as an array of ValueTuples, or as a third type in a type-list? The latter of takes precedence here. Solution? You Tuple!(int, uint, char) instead (see std.typecons for Tuple introduction): import std.typecons; Tuple!(int, uint, char) x0; pragma(msg, typeof(x0).stringof); // prints: Tuple!(int,uint,char) Tuple!(int, uint, char)[2] x1; pragma(msg, typeof(x1).stringof); // prints: Tuple!(int,uint,char)[2u] ValueTuple!(int, uint, char)[2] x2; pragma(msg, typeof(x2).stringof); // prints: char
Jun 04 2008
next sibling parent BCS <ao pathlink.com> writes:
Reply to Koroskin,

I'm not sure what you are getting at. What I was getting at is that T[#] 
can be interpreted in two ways depending on if T is or is not a tuple. In 
some odd corner cases this could end up causing some compile time bugs and 
also it generates some complexities in parsing D. There really is no way 
to fix it. It's just an odd corner of D.
Jun 04 2008
prev sibling parent "Koroskin Denis" <2korden gmail.com> writes:
Your example was somewhat a bit compilated and not clear, I tried to  
simplify and analyze it (to get a better knowledge of the problem to  
myself and others). That was my point of the post.

Back to the topic, you could submit a bug report, if you feel that it's  
not the way it should be in D.

ValueTuple!(int, uint, char)[2] currently gives char
Tuple!(int, uint, char)[2] currently gives a static array of Tuple!(int,  
uint, char)

This inconsistancy could be fixed by treating ValueTuple!(int, uint,  
char)[2] as an static array of ValueTuples, I don't see any problem here.  
Type of third value in a ValueTuple can be access in other (more generic)  
way:

ValueTuple!(int, uint, char) x;
alias typeof(x[2]) Type;	// works for both Tuple and ValueTuple
Jun 05 2008