www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Error: template instance does not match template declaration

reply ref2401 <refactor24 gmail.com> writes:
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
}
Mar 29 2016
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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
Mar 29 2016
next sibling parent ref2401 <refactor24 gmail.com> writes:
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.
Mar 29 2016
prev sibling parent ref2401 <refactor24 gmail.com> writes:
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.
Mar 30 2016