How to know if opSlice is defined for a type (including built-in types)?

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jan 20 07:25:10 PST 2016


On Wednesday, January 20, 2016 13:06:00 chardetm via Digitalmars-d-learn wrote:
> Anyone who has the same problem: I found
> std.range.primitives.hasSlicing
> (https://dlang.org/phobos/std_range_primitives.html#hasSlicing)
> which does exactly what I want!

Note that because strings are treated as ranges of dchar regardless of what
their actual character type is, arrays of char and wchar (so-called "narrow"
strings) are not consider to have slicing or random access by the traits in
std.range. So, hasSlicing!string is false, though for anything other than an
array of char or wchar, it will do what you're looking for, whereas for
arrays of char or wchar, you really shouldn't be using the slice operator on
them without knowing that they're what you're operating on so that you take
the Unicode issues into account correctly.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list