www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - ElementType!(Range) problem

reply Ishan Thilina <ishanthilina gmail.com> writes:
I can use the ElementType() template in the std.rane library to find the type
of the elements of a range,isn't it? But I cant compile the following programme.

I get a "untitled.d(32): Error: template instance ElementType!(listR) does not
match template declaration ElementType(R)" error.

What could be the problem ?
--------------------------------------------------------------------------

import std.stdio;
import std.range;
import std.container;

int main(char[][] args)
{
	auto list=SList!(int)(1,2,3);
	auto listR=list.opSlice();
	writefln("%s",ElementType!(listR));


	return 0;
}
--------------------------------------------------------------

Thank you...!
Apr 05 2011
parent reply Philippe Sigaud <philippe.sigaud gmail.com> writes:
 I get a "untitled.d(32): Error: template instance ElementType!(listR) doe=

 match template declaration ElementType(R)" error.

 What could be the problem ?
 -------------------------------------------------------------------------=

 import std.stdio;
 import std.range;
 import std.container;

 int main(char[][] args)
 {
 =C2=A0 =C2=A0 =C2=A0 =C2=A0auto list=3DSList!(int)(1,2,3);
 =C2=A0 =C2=A0 =C2=A0 =C2=A0auto listR=3Dlist.opSlice();
 =C2=A0 =C2=A0 =C2=A0 =C2=A0writefln("%s",ElementType!(listR));


 =C2=A0 =C2=A0 =C2=A0 =C2=A0return 0;
 }
 --------------------------------------------------------------

ElementType acts on types. It takes a type and 'returns' (compiles to, actually) another type. You need to give it typeof(listR). Then, as ElementType!(typeof(listR)) is a type, you cannot pass it to writeln. Use .stringof to go from the type to a string representation of its name. So: writefln("%s", ElementType!(typeof(listR)).stringof); ElementType!(typeof(listR)) is a type like any other. You can create a variable with it: ElementType!(typeof(listR)) elem; Btw, .opSlice() is the name of the '[]' operator. You previous line can be written like this: auto listR =3D list[]; Philippe
Apr 05 2011
parent Ishan Thilina <ishanthilina gmail.com> writes:
--Philippe wrote::

ElementType acts on types. It takes a type and 'returns' (compiles to,
actually) another type. You need to give it typeof(listR).
Then, as ElementType!(typeof(listR)) is a type, you cannot pass it to
writeln. Use .stringof to go from the type to a string representation
of its name.

So:

writefln("%s", ElementType!(typeof(listR)).stringof);

ElementType!(typeof(listR)) is a type like any other. You can create a
variable with it:

ElementType!(typeof(listR)) elem;

Got it!. Thanks. I faced this confusion because of the description for "ElementType" which is available in http://d-programming-language.org/phobos/std_range.html. It says, " template ElementType(R): The element type of R. R does not have to be a range. The element type is determined as the type yielded by r.front for an object r or type R. For example, ElementType!(T[]) is T. If R is not a range, ElementType!R is void. " So I thought that just passing ElementType!listR was adequate. :s
Apr 06 2011