I've been in need of getting the type of the contents of an array. Phobos includes a function to get the type of the pointer target.
I propose a simple template, arrayTarget to std.traits to implement this functionality.
The following works under DMD 2.049, Linux/x86
template arrayTarget(T:T[]) {
alias T arrayTarget;
}
unittest {
assert(is(arrayTarget!(int[5][]) == int[5]));
assert(is(arrayTarget!(int[5]) == int));
assert(is(arrayTarget!(int[]) == int));
assert(is(arrayTarget!(int[][]) == int[]));
assert(__traits(compiles,arrayTarget!(int)) == false);
assert(__traits(compiles,arrayTarget!(int *)) == false);
}
Comment #1 by yebblies — 2011-06-15T23:16:49Z
Is this covered by ElementType/EncodingType?
Comment #2 by andrej.mitrovich — 2012-12-18T13:19:11Z
(In reply to comment #1)
> Is this covered by ElementType/EncodingType?
Yes, except it's more generic and works with pointers too.
Comment #3 by verylonglogin.reg — 2013-02-17T23:30:43Z
Not fixed.
The enhancement is about arrays, not ranges. Phobos misses a lot of traits in regard to arrays. This one included.
Comment #4 by yebblies — 2013-02-17T23:49:27Z
Arrays _are_ ranges. This functionality is already in phobos.
Comment #5 by verylonglogin.reg — 2013-02-18T02:42:03Z
(In reply to comment #4)
> Arrays _are_ ranges.
No. E.g. `int[1]` and `const int[]` are not ranges.
> This functionality is already in phobos.
`char[]` can be just an array of `char`-s without any thoughts about encoding and combining that `char`-s into a string so neither `ElementType` nor `ElementEncodingType` are applicable.
Comment #6 by andrej.mitrovich — 2013-02-18T09:17:34Z
(In reply to comment #5)
> (In reply to comment #4)
> > Arrays _are_ ranges.
>
> No. E.g. `int[1]` and `const int[]` are not ranges.
Both ElementType and ElementEncodingType work with these.
> `char[]` can be just an array of `char`-s without any thoughts about encoding
> and combining that `char`-s into a string so neither `ElementType` nor
> `ElementEncodingType` are applicable.
I don't understand what you mean. A code example would help.
Comment #7 by verylonglogin.reg — 2013-10-26T01:11:57Z