How to know if opSlice is defined for a type (including built-in types)?
chardetm via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Jan 20 02:41:17 PST 2016
Hi everyone,
I was wondering if it was possible to know at compile time if
opSlice is defined for a type, including built-in types like
"string". Here is the code I have:
import std.stdio;
struct Test {
Test opSlice(size_t i, size_t j) {
return this; // We don't care what it returns
}
}
void main () {
string s = "Hello";
auto s2 = s[0..2];
static if (is(typeof(&string.opSlice))) writeln("Ok string");
Test t;
auto t2 = t[0..2];
static if (is(typeof(&Test.opSlice))) writeln("Ok Test");
}
Output:
Ok Test
How to make it work for "string" too (if possible)? And is there
a way to differentiate "opSlice()" from "opSlice(size_t, size_t)"
(or "opSlice(T, T)" for any T)?
Thank you in advance!
More information about the Digitalmars-d-learn
mailing list