type of array element, type of assoz key?
Frits van Bommel
fvbommel at REMwOVExCAPSs.nl
Sun Aug 5 00:41:23 PDT 2007
dennis luehring wrote:
> how can i get the element type of an array at compiletime?
>
> example:
> int test[5] --> int
> double test[] --> double
>
> and what is the type of the key of an assoz. array?
>
> int test[char[]] --> int, char[]
> double test[int] --> double, int
(This sort of post is more appropriate in digitalmars.D.learn, followups
set)
typeof to the rescue:
-----
import std.stdio;
void main() {
// (typeid is just wrapped around the types to make them printable)
// Element type of regular arrays: typeof(arr[0]);
{
int test[5];
writefln(typeid( typeof(test[0]) ));
}
writefln();
{
double test[];
writefln(typeid( typeof(test[0]) ));
}
writefln();
// Assoc. arrays:
// key type == typeof(arr.keys[0])
// value type == typeof(arr.values[0])
{
int test[char[]];
writefln(typeid( typeof(test.values[0]) ));
writefln(typeid( typeof(test.keys[0]) ));
}
writefln();
{
double test[int];
writefln(typeid( typeof(test.values[0]) ));
writefln(typeid( typeof(test.keys[0]) ));
}
}
-----
output:
=====
int
double
int
char[]
double
int
=====
More information about the Digitalmars-d
mailing list