www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Passing anonymous templated functions as template parameters

reply pineapple <meapineapple gmail.com> writes:
Here's a simple code example to illustrate what I expected to 
work and didn't - is this a mistake in my syntax or a limitation 
of the language?

     template SomeTemplate(alias func){
         auto templatefunc(T)(int x){
             return func!T(x);
         }
     }

     // Valid
     auto somefunc(T)(int x){
         return cast(T) x;
     }
     alias fn1 = SomeTemplate!somefunc;

     // Not valid
     alias fn2 = SomeTemplate!(
         (T)(int x){return cast(T) x;}
     );
Jun 15 2016
parent reply Basile B. <b2.temp gmx.com> writes:
On Wednesday, 15 June 2016 at 22:27:38 UTC, pineapple wrote:
 Here's a simple code example to illustrate what I expected to 
 work and didn't - is this a mistake in my syntax or a 
 limitation of the language?

     template SomeTemplate(alias func){
         auto templatefunc(T)(int x){
             return func!T(x);
         }
     }

     // Valid
     auto somefunc(T)(int x){
         return cast(T) x;
     }
     alias fn1 = SomeTemplate!somefunc;

     // Not valid
     alias fn2 = SomeTemplate!(
         (T)(int x){return cast(T) x;}
     );
This syntax passes: alias fn2(T) = SomeTemplate!((int x){return cast(T) x;});
Jun 15 2016
next sibling parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 6/15/16 7:52 PM, Basile B. wrote:
 On Wednesday, 15 June 2016 at 22:27:38 UTC, pineapple wrote:
 Here's a simple code example to illustrate what I expected to work and
 didn't - is this a mistake in my syntax or a limitation of the language?

     template SomeTemplate(alias func){
         auto templatefunc(T)(int x){
             return func!T(x);
         }
     }

     // Valid
     auto somefunc(T)(int x){
         return cast(T) x;
     }
     alias fn1 = SomeTemplate!somefunc;

     // Not valid
     alias fn2 = SomeTemplate!(
         (T)(int x){return cast(T) x;}
This isn't valid syntax. I don't think you can create anonymous templates. I could be wrong. -Steve
Jun 15 2016
prev sibling parent Basile B. <b2.temp gmx.com> writes:
On Wednesday, 15 June 2016 at 23:52:56 UTC, Basile B. wrote:
 On Wednesday, 15 June 2016 at 22:27:38 UTC, pineapple wrote:
 Here's a simple code example to illustrate what I expected to 
 work and didn't - is this a mistake in my syntax or a 
 limitation of the language?

     template SomeTemplate(alias func){
         auto templatefunc(T)(int x){
             return func!T(x);
         }
     }

     // Valid
     auto somefunc(T)(int x){
         return cast(T) x;
     }
     alias fn1 = SomeTemplate!somefunc;

     // Not valid
     alias fn2 = SomeTemplate!(
         (T)(int x){return cast(T) x;}
     );
This syntax passes: alias fn2(T) = SomeTemplate!((int x){return cast(T) x;});
I didn't try to instanciate previously. It works a bit with a lambda to the extent that the alias has the template parameter list. import std.stdio; template A(alias func) { auto a(T)(int x) { return func!T(x); } } alias spec(T) = A!(x => (cast(T) x)); void main(string[] args) { writeln((spec!byte).a!int(257)); // 1 }
Jun 15 2016