Discussion:
Patterns for functions in template parameters
Max Samukha via Digitalmars-d-learn
2014-10-23 09:23:43 UTC
Permalink
If I remember correctly, at some point a syntax was introduced
for pattern-matching functions passed to templates. Something
like:

template Foo(B(A) foo, A, B)
{
}

alias f = Foo!((int x) => x % 2 == 0);

That would instantiate Foo with B == bool, A == int and foo bound
to the lambda.

The compiler has rejected all syntax variations I have tried and
the specs is not helpful. Does anybody know if the feature exists
and what is the syntax exactly?
Kagamin via Digitalmars-d-learn
2014-10-23 11:25:00 UTC
Permalink
Maybe, argument deduction?

template Foo(T: T[U], U)
{
...
}

Foo!(int[long]) // instantiates Foo with T set to int, U set to
long
Max Samukha via Digitalmars-d-learn
2014-10-23 18:28:04 UTC
Permalink
Post by Kagamin via Digitalmars-d-learn
Maybe, argument deduction?
template Foo(T: T[U], U)
{
...
}
Foo!(int[long]) // instantiates Foo with T set to int, U set
to long
Yes, but for a value template parameter.

template Foo(int[long] a);
{
}
Foo!([1: 2]); // ok, this passes.


Now I'd like to loosen the template so it accepts AAs of any type:

template Foo(T[U] a /* forall T, U */)
{
}


And then unary functions of any type:

template Foo(T a(U) /* forall T, U */)
{
}


In other words, to move the run-time parameter below to the
template parameter list:

void foo(T, U)(T delegate(U) a)
{
}


Or in other other words (for any "callables"):

template Foo(alias a) if (isCallable!a && ParameterTypes!a.length
== 1)
{
alias T = ReturnType!a;
alias U = ParameterTypes!a[0];
}


I vaguely remember some syntax tweaks were introduced, presumably
for that effect. Trying to find out what they were exactly. I
might have dreamt it as well.
Kagamin via Digitalmars-d-learn
2014-10-24 08:53:04 UTC
Permalink
maybe
template Foo(T a, T: T[U], U)

Continue reading on narkive:
Loading...