digitalmars.D.learn - Finding ElementType
- Bastiaan Veelo (39/39) Jan 07 2018 The learn forum is great not only for asking questions, but also
- Adam D. Ruppe (7/13) Jan 07 2018 see ElementEncodingType
- Bastiaan Veelo (2/15) Jan 07 2018 Thanks.
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
Jan 07 2018
On Sunday, 7 January 2018 at 12:40:22 UTC, Bastiaan Veelo wrote:1) Should we have a reference in the docs for std.traits to std.range.primitive : ElementType?wouldn't hurt i guess2) Should phobos contain a version without the special narrow string behavior?see ElementEncodingType http://dpldocs.info/experimental-docs/std.range.primitives.ElementEncodingType.html3) Are there other differences between my version and the phobos version?The Phobos one works on any range, not just built-in arrays. It also returns an answer (void) if you pass it something that has no element type, whereas yours would be a compile error.
Jan 07 2018
On Sunday, 7 January 2018 at 13:50:23 UTC, Adam D. Ruppe wrote:On Sunday, 7 January 2018 at 12:40:22 UTC, Bastiaan Veelo wrote:Thanks.1) Should we have a reference in the docs for std.traits to std.range.primitive : ElementType?wouldn't hurt i guess2) Should phobos contain a version without the special narrow string behavior?see ElementEncodingType http://dpldocs.info/experimental-docs/std.range.primitives.ElementEncodingType.html3) Are there other differences between my version and the phobos version?The Phobos one works on any range, not just built-in arrays. It also returns an answer (void) if you pass it something that has no element type, whereas yours would be a compile error.
Jan 07 2018