www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Template arguments produce unidentified identifier

reply Rufus Smith <RufusSmith indi.com> writes:
I have complex template that uses a mixin to solve some problems. 
The mixin produces the error.  I thought template's were added in 
to the scope of the call? I guess the mixin is inserted before 
this happens. That isn't good ;/

Here is one place the error happens

	mixin("alias Func = extern("~functionLinkage!Q~") 
"~(ReturnType!Q).stringof~" 
function"~(Erase!(Parameters!Q)).stringof~";");

The mixin is required to get the proper linkage, as no other 
method has been found to do that. Everything works with built in 
types.

I saw somewhere that vibe.d had some way around this but the 
links are dead.
Jul 19 2016
next sibling parent Rufus Smith <RufusSmith indi.com> writes:
If it's not clear, I have to import the proper identifiers but 
every use of the template would require the user to add their 
import. Obviously not the way to go.
Jul 19 2016
prev sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
Take a read of this:

http://stackoverflow.com/a/32621854/1457000

The short of it is don't mixin stringof. Instead, mixin the 
actual template itself.

The functionLinkage might need to be string, but the types should 
remain literal. So try this:

mixin("alias Func = extern("~functionLinkage!Q~") (ReturnType!Q) 
function (Erase!(Parameters!Q));");


or something like that - don't concatenate strings of those, just 
make the string itself still say ReturnType!Q etc when you mix 
in. Then the scope will be correct.
Jul 19 2016
parent Rufus Smith <RufusSmith indi.com> writes:
On Wednesday, 20 July 2016 at 01:48:31 UTC, Adam D. Ruppe wrote:
 Take a read of this:

 http://stackoverflow.com/a/32621854/1457000

 The short of it is don't mixin stringof. Instead, mixin the 
 actual template itself.

 The functionLinkage might need to be string, but the types 
 should remain literal. So try this:

 mixin("alias Func = extern("~functionLinkage!Q~") 
 (ReturnType!Q) function (Erase!(Parameters!Q));");


 or something like that - don't concatenate strings of those, 
 just make the string itself still say ReturnType!Q etc when you 
 mix in. Then the scope will be correct.
Thanks, it did work with some modification: mixin("alias Func = extern("~functionLinkage!Q~") ReturnType!Q function(Erase!(Parameters!Q));"); No parenthesis around ReturnType or strange errors happen.
Jul 19 2016