digitalmars.D.learn - Patterns for functions in template parameters
- Max Samukha (12/12) Oct 23 2014 If I remember correctly, at some point a syntax was introduced
- Kagamin (7/7) Oct 23 2014 Maybe, argument deduction?
- Max Samukha (29/36) Oct 23 2014 Yes, but for a value template parameter.
- Kagamin (2/2) Oct 24 2014 maybe
- Max Samukha (2/4) Oct 24 2014 No luck. Error: undefined identifier T
- ketmar via Digitalmars-d-learn (10/10) Oct 24 2014 On Thu, 23 Oct 2014 18:28:04 +0000
- "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> (12/23) Oct 24 2014 I thought the same thing, but it doesn't compile with DMD from
- ketmar via Digitalmars-d-learn (4/23) Oct 24 2014 Foo is a function, not declaration. put in in main(), for example:
- "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> (3/28) Oct 24 2014 Now I'm feeling really stupid :-P
- ketmar via Digitalmars-d-learn (5/18) Oct 24 2014 On Fri, 24 Oct 2014 17:05:57 +0300
- Max Samukha (11/14) Oct 24 2014 What I meant was your example with the delegate parameter moved
- Max Samukha (3/5) Oct 24 2014 Nor does it allow passing delegates to value parameters, only
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?
Oct 23 2014
Maybe, argument deduction?
template Foo(T: T[U], U)
{
...
}
Foo!(int[long]) // instantiates Foo with T set to int, U set to
long
Oct 23 2014
On Thursday, 23 October 2014 at 11:25:01 UTC, Kagamin wrote:
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.
Oct 23 2014
On Friday, 24 October 2014 at 08:53:05 UTC, Kagamin wrote:maybe template Foo(T a, T: T[U], U)No luck. Error: undefined identifier T
Oct 24 2014
On Thu, 23 Oct 2014 18:28:04 +0000
Max Samukha via Digitalmars-d-learn <digitalmars-d-learn puremagic.com>
wrote:
um-hm... maybe this:
void Foo(T, U) (T delegate (U) a) {
// here T is bool, U is int for the following sample
import std.stdio;
writeln(a(3));
}
Foo((int x) =3D> x%2 =3D=3D 0);
Oct 24 2014
On Friday, 24 October 2014 at 14:06:08 UTC, ketmar via
Digitalmars-d-learn wrote:
On Thu, 23 Oct 2014 18:28:04 +0000
Max Samukha via Digitalmars-d-learn
<digitalmars-d-learn puremagic.com>
wrote:
um-hm... maybe this:
void Foo(T, U) (T delegate (U) a) {
// here T is bool, U is int for the following sample
import std.stdio;
writeln(a(3));
}
Foo((int x) => x%2 == 0);
I thought the same thing, but it doesn't compile with DMD from
Git:
test.d(7): Error: function declaration without return type. (Note
that constructors are always named 'this')
test.d(7): Error: found '=>' when expecting ')'
test.d(7): Error: no identifier for declarator Foo(int x)
test.d(7): Error: semicolon expected following function
declaration
test.d(7): Error: no identifier for declarator x
test.d(7): Error: Declaration expected, not '%'
Oct 24 2014
On Fri, 24 Oct 2014 16:00:41 +0000 via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:On Friday, 24 October 2014 at 14:06:08 UTC, ketmar via=20 Digitalmars-d-learn wrote:Foo is a function, not declaration. put in in main(), for example: void main () { Foo((int x) =3D> x%2 =3D=3D 0); }On Thu, 23 Oct 2014 18:28:04 +0000 Max Samukha via Digitalmars-d-learn=20 <digitalmars-d-learn puremagic.com> wrote: um-hm... maybe this: void Foo(T, U) (T delegate (U) a) { // here T is bool, U is int for the following sample import std.stdio; writeln(a(3)); } Foo((int x) =3D> x%2 =3D=3D 0);=20 I thought the same thing, but it doesn't compile with DMD from=20 Git:
Oct 24 2014
On Friday, 24 October 2014 at 16:12:48 UTC, ketmar via Digitalmars-d-learn wrote:On Fri, 24 Oct 2014 16:00:41 +0000 via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:Now I'm feeling really stupid :-POn Friday, 24 October 2014 at 14:06:08 UTC, ketmar via Digitalmars-d-learn wrote:Foo is a function, not declaration. put in in main(), for example: void main () { Foo((int x) => x%2 == 0); }On Thu, 23 Oct 2014 18:28:04 +0000 Max Samukha via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote: um-hm... maybe this: void Foo(T, U) (T delegate (U) a) { // here T is bool, U is int for the following sample import std.stdio; writeln(a(3)); } Foo((int x) => x%2 == 0);I thought the same thing, but it doesn't compile with DMD from Git:
Oct 24 2014
On Fri, 24 Oct 2014 17:05:57 +0300
ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com>
wrote:
On Thu, 23 Oct 2014 18:28:04 +0000
Max Samukha via Digitalmars-d-learn
<digitalmars-d-learn puremagic.com> wrote:
=20
um-hm... maybe this:
=20
void Foo(T, U) (T delegate (U) a) {
// here T is bool, U is int for the following sample
import std.stdio;
writeln(a(3));
}
=20
Foo((int x) =3D> x%2 =3D=3D 0);
sorry if this is not what you mean, template magic sometimes scares me
and i'm loosing my mind. ;-)
Oct 24 2014
On Friday, 24 October 2014 at 14:13:10 UTC, ketmar via Digitalmars-d-learn wrote:sorry if this is not what you mean, template magic sometimes scares me and i'm loosing my mind. ;-)What I meant was your example with the delegate parameter moved to the template list: template Foo(T delegate(U) a, T, U) {} Foo!((int x) => true); // T == bool, U == int Or generalized to functions (and maybe "functors" in the unfortunate C++ sense): template Foo(T(U) a, T, U) {} Apparently, D doesn't allow type variables in value parameters at all.
Oct 24 2014
On Friday, 24 October 2014 at 17:08:00 UTC, Max Samukha wrote:Apparently, D doesn't allow type variables in value parameters at all.Nor does it allow passing delegates to value parameters, only alias parameters.
Oct 24 2014









"Max Samukha" <maxsamukha gmail.com> 