Can you introspect predicate arity and if it's an equality predicate?
aliak
something at something.com
Fri Jan 12 00:16:07 UTC 2018
Hi, so basically is there a way to:
void func(alias pred = null, Range)(Range range) {
// 1) check if pred(ElementType!Range.init,
ElementType!Range.init) is equality
// 2) check if isUnary!pred
// 3) check if isBinary!pred
}
I think maybe the isUnary or isBinary may not work unless it
takes an extra parameter that gives it some context, i.e.
isBinary!(pred, T, U) where T and U are the parameter types.
Currently I have this for an isUnary
template isUnary(alias pred) {
static if (is(typeof(pred) : string))
{
enum isUnary = is(typeof(unaryFun!pred(0)));
}
else static if (is(Parameters!pred F) && F.length == 1)
{
enum isUnary = true;
}
else
{
enum isUnary = false;
}
}
But that does not seem to work in this situation:
isUnary!(a => a) // returns false
It won't work if I do isUnary!"a(2)" for e.g, in the case that T
is callable or isUnary!"a.x" in the case that "a" is a struct
with a member x ... just to name a couple of cases.
If I give context then all's good:
enum isUnaryOver = is(typeof(unaryFun!pred(T.init))); // works
isEqualityFunction Im not sure how to go about. So open to any
ideas, or if there're already some traits or something I can use.
I was thinking I could do:
alias E = // some element type
static if (isBinary!(pred, E) && binaryFun!pred(E.init, E.init)
== true)
{
// is equality ... but the for double.init is NaN == NaN ?
// and probably some other things I'm not thinking about.
}
Advice and/or suggestions?
Cheers
PS: the purpose of the isEqualityFunction is to determine whether
I should use the predicate for sorting or for just for searching,
so:
f!"a < b"(range) // range is sorted and then operated on
f!"a == b"(range) // range is not sorted and is operated on by
leveraging pred
More information about the Digitalmars-d-learn
mailing list