www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Why this template code does not compile?

reply Victor Porton <porton narod.ru> writes:
Why this template code does not compile?

///
module runnable;

import std.meta;
import std.typecons;

template FieldInfo(argT, string argName) {
     template FieldInfo(Nullable!argT argDefault = 
Nullable!argT()) {
     }
}

alias processFields(T, string name) =
     AliasSeq!(FieldInfo!(T, name)());

void main(string[] args)
{
     alias Fields = processFields!(int, "x");
}
///

/tmp/temp_7F58BE2F0150.d(12,34): Error: template 
`runnable.FieldInfo!(int, "x").FieldInfo` cannot deduce function 
from argument types `!()()`, candidates are:
/tmp/temp_7F58BE2F0150.d(7,5):        `runnable.FieldInfo!(int, 
"x").FieldInfo(Nullable!int argDefault = Nullable!argT())`
/tmp/temp_7F58BE2F0150.d(16,20): Error: template instance 
`runnable.processFields!(int, "x")` error instantiating

LDC - the LLVM D compiler (1.11.0):
   based on DMD v2.081.2 and LLVM 6.0.1
Mar 13 2019
parent Paul Backus <snarwin gmail.com> writes:
On Wednesday, 13 March 2019 at 17:38:02 UTC, Victor Porton wrote:
 Why this template code does not compile?

 ///
 module runnable;

 import std.meta;
 import std.typecons;

 template FieldInfo(argT, string argName) {
     template FieldInfo(Nullable!argT argDefault = 
 Nullable!argT()) {
     }
 }

 alias processFields(T, string name) =
     AliasSeq!(FieldInfo!(T, name)());
The inner `FieldInfo` template isn't a function, but by adding a second set of parentheses after `(T, name)`, you're attempting to call it as one. If you want to instantiate the inner template with its default argument, you can use `std.meta.Instantiate`: alias processFields(T, string name) = AliasSeq!(Instantiate!(FieldInfo!(T, name)));
Mar 13 2019