Can you tell if an template alias parameter is of a specific template?
aliak
something at something.com
Fri Aug 3 21:53:08 UTC 2018
On Friday, 3 August 2018 at 19:10:45 UTC, Hakan Aras wrote:
> I don't think you can distinguish between entities evaluated
> through different templates. TemplateOf will paradoxically not
> work on pure templates of the form "template X() {}" only
> things like template functions and template structs. isSame,
> is, and isInstanceOf will only work on the fully evaluated type
> or alias (pred) not the template that was used to evaluate it.
>
> If I understood your usecase correctly, you could do something
> like this though:
>
> https://run.dlang.io/is/VwZoAx
>
> That is use an enum as a template parameter instead of wrapping
> the predicate in a template.
You did understand and yep that'd work as well! I was not too
happy about the extra parameter though and it'd be nice to have
the information in the type. But I think I figured it out:
struct eq(alias _pred) {
alias pred = _pred;
}
struct lt(alias _pred) {
alias pred = _pred;
}
void main() {
import std.stdio: writeln;
static struct S(alias pred) {
auto f(int a, int b) {
static if (is(pred : eq!p, p...)) {
return p[0](a, b);
} else static if (is(pred : lt!p, p...)) {
return !p[0](a, b) && !p[0](b, a);
} else {
import std.functional: binaryFun;
return binaryFun!pred(a, b);
}
}
}
alias a = eq!((a, b) => a == b);
alias b = lt!((a, b) => a < b);
S!(a)().f(1, 1).writeln;
S!(b)().f(1, 1).writeln;
S!"a == b"().f(1, 1).writeln;
}
More information about the Digitalmars-d-learn
mailing list