Finding ElementType
Bastiaan Veelo
Bastiaan at Veelo.net
Sun Jan 7 12:40:22 UTC 2018
The learn forum is great not only for asking questions, but also
for learning from the questions by others: I just learned about
the existence of std.range.primitives: ElementType, a function
that I have looked for in phobos before, without finding it. I
had expected this to be in std.traits or __traits.
So I implemented my own:
```
template arrayElementType(T : T[])
{
alias arrayElementType = T;
}
unittest
{
static assert (is(arrayElementType!(int[]) == int));
}
```
For reference, this is the phobos version:
```
template ElementType(R)
{
static if (is(typeof(R.init.front.init) T))
alias ElementType = T;
else
alias ElementType = void;
}
```
As far as I can see these are largely equivalent, except that the
phobos version has special behavior for narrow strings [1]. This
special behavior is actually unwanted in my case.
The following questions pop up:
1) Should we have a reference in the docs for std.traits to
std.range.primitive : ElementType?
2) Should phobos contain a version without the special narrow
string behavior?
3) Are there other differences between my version and the phobos
version?
Thanks,
Bastiaan.
[1] https://dlang.org/phobos/std_range_primitives.html#ElementType
More information about the Digitalmars-d-learn
mailing list