Template instantiation syntax

Michel Fortin michel.fortin at michelf.com
Sat Oct 11 12:05:17 PDT 2008


On 2008-10-11 02:22:22 -0400, Walter Bright <newshound1 at digitalmars.com> said:

> Sergey Gromov wrote:
>> so that even a!b!c!(x,y) works as a!(b!(c!(x,y))), but a!(b)!(c) is not 
>> accepted.
> 
> I don't think it's a good idea to say that a!(b)!(c) is never allowed. 
> It intuitively means that a!(b) is aliased to another template.

There is a couple of those in the D/Objective-C bridge. Basically, a 
few function templates are defined like this:

	template invoke(ReturnType, char[] methodName)
	{
		ReturnType invoke(Args...)(Args args)
		{
			// call the method while wrapping all elements as necessary
		}
	}

It's generally used like this:

	invoke!(void, "setColor")(color);

The nested-template trick avoids having to write all the function's 
arguments types since for the inner template, arguments are deduced 
from the function call. But if you want to be explicit about the 
argument types, you can do a chained template:

	invoke!(void, "setColor")!(NSColor)(color);

Isn't that a funny line? Now, how readable is this one:

	invoke!(void, "setColor")!NSColor(color);

?

 - - -

I'd like it very much if templates such as these could work with only 
one argument list, such as:

	invoke(void, "setColor", color);

It makes this example simpler to read, because now you don't have to 
bother about all the technicalities of what is a template parameter and 
what is a runtime parameter. It could probably simplify the definition 
too. Here is what I have in mind for the function declaration:

	ReturnType invoke(typename ReturnType, static char[] methodName, 
auto(Args...) args)
	{
		...
	}

-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/




More information about the Digitalmars-d mailing list