Thread overview | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
October 23, 2014 Patterns for functions in template parameters | ||||
---|---|---|---|---|
| ||||
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? |
October 23, 2014 Re: Patterns for functions in template parameters | ||||
---|---|---|---|---|
| ||||
Posted in reply to Max Samukha | Maybe, argument deduction? template Foo(T: T[U], U) { ... } Foo!(int[long]) // instantiates Foo with T set to int, U set to long |
October 23, 2014 Re: Patterns for functions in template parameters | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kagamin | 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.
|
October 24, 2014 Re: Patterns for functions in template parameters | ||||
---|---|---|---|---|
| ||||
Posted in reply to Max Samukha | maybe template Foo(T a, T: T[U], U) |
October 24, 2014 Re: Patterns for functions in template parameters | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kagamin | 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
|
October 24, 2014 Re: Patterns for functions in template parameters | ||||
---|---|---|---|---|
| ||||
Posted in reply to Max Samukha Attachments: | 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); |
October 24, 2014 Re: Patterns for functions in template parameters | ||||
---|---|---|---|---|
| ||||
Attachments: | 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: > > 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); sorry if this is not what you mean, template magic sometimes scares me and i'm loosing my mind. ;-) |
October 24, 2014 Re: Patterns for functions in template parameters | ||||
---|---|---|---|---|
| ||||
Posted in reply to ketmar | 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 '%'
|
October 24, 2014 Re: Patterns for functions in template parameters | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marc Schütz Attachments: | 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 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: Foo is a function, not declaration. put in in main(), for example: void main () { Foo((int x) => x%2 == 0); } |
October 24, 2014 Re: Patterns for functions in template parameters | ||||
---|---|---|---|---|
| ||||
Posted in reply to ketmar | 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.
|
Copyright © 1999-2021 by the D Language Foundation