Thread overview
Error: template instance does not match template declaration
Mar 29, 2016
ref2401
Mar 29, 2016
Ali Çehreli
Mar 29, 2016
ref2401
Mar 30, 2016
ref2401
March 29, 2016
OS: Win 8.1 Pro
DMD: v2.070.0
cmd: dmd main.d -ofconsole-app.exe -debug -unittest -wi

Code is successfully compiled until I uncomment test1 function call in the main.
If it's uncommented I get the following compile-time error:

main.d(58): Error: template instance impl!(string, bool, ["y":true, "n":false]) does not match template declaration impl(S, T, Color[string] dict)(S arg)
main.d(62): Error: template instance mainentry.test1!string error instantiating

What have I done wrong?
Thank you.

import std.stdio;

enum Color { RED, GREEN }

private void impl(S, T, T[string] dict)(S arg) {
	writeln("S: ", S.stringof);
	writeln("T: ", T.stringof);
	writeln("dict: ", dict);
}

unittest {
	enum COLOR_MAP = [
		"r": Color.RED,
		"g": Color.GREEN
	];

	void test0(S)(S arg) {
		impl!(S, Color, COLOR_MAP)(arg);
	}

	test0("000");
}

void test1(S)(S arg) {
	impl!(S, bool, ["y": true, "n": false])(arg);
}

void main(string[] args) {
	// test1("111"); // if you uncomment this line you'll get a compile-time error
}
March 29, 2016
On 03/29/2016 11:21 AM, ref2401 wrote:

> private void impl(S, T, T[string] dict)(S arg) {
>      writeln("S: ", S.stringof);
>      writeln("T: ", T.stringof);
>      writeln("dict: ", dict);

So, dict is a template value parameter of type T[string]. I don't think you can use an associative array as a template value parameter. (Can we?)

However, it is possible to use anything as an alias template parameter.

If it's acceptable in your case, the following works:

private void impl(S, T, alias dict)(S arg)
        if (is (typeof(dict) == T[S])) {

(Or T[string]).

Ali

March 29, 2016
On Tuesday, 29 March 2016 at 18:29:27 UTC, Ali Çehreli wrote:
> On 03/29/2016 11:21 AM, ref2401 wrote:
>
> So, dict is a template value parameter of type T[string]. I don't think you can use an associative array as a template value parameter. (Can we?)
>
> However, it is possible to use anything as an alias template parameter.
>
> If it's acceptable in your case, the following works:
>
> private void impl(S, T, alias dict)(S arg)
>         if (is (typeof(dict) == T[S])) {
>
> (Or T[string]).
>
> Ali

alias works for me, thank you.
March 30, 2016
On Tuesday, 29 March 2016 at 18:29:27 UTC, Ali Çehreli wrote:
> So, dict is a template value parameter of type T[string]. I don't think you can use an associative array as a template value parameter. (Can we?)

Found this in D language reference:
https://dlang.org/spec/template.html#template_value_parameter
Template value arguments can be integer values, floating point values, nulls, string values,
array literals of template value arguments, associative array literals of template value arguments...

So yes, associative arrays as template parameters are supposed to work.