Implicit conversion to base interface doesn't work inside initialization of AA of objects

Uranuz via Digitalmars-d digitalmars-d at puremagic.com
Thu Jul 17 07:21:39 PDT 2014


Seems that I saw similar post but about plain arrays somewhere 
but currently I can't find it. The topic is that when I 
initialize associative array of interfaces with AA-literal 
consisting of derived class objects I get compiler error.

import std.stdio;

interface IBase
{
	
}

class Impl(T): IBase
{
	T value;
}


void main()
{
	IBase[string] fldFormats = [ 	
		"Key": new Impl!(uint),
		"Name": new Impl!(string),
		"Percent": new Impl!(double)
	];
}

As a result I get this interesting error message:
Compilation output:
/d780/f916.d(22): Error: cannot implicitly convert expression 
(new Impl) of type object.Object to f916.Impl!(double).Impl
/d780/f916.d(22): Error: cannot implicitly convert expression 
(new Impl) of type object.Object to f916.Impl!(double).Impl
/d780/f916.d(18): Error: cannot implicitly convert expression 
(["Key":(__error),"Name":(__error),"Percent":new Impl]) of type 
Impl[string] to IBase[string]

I'm very glad that compiler now can implicitly infer some 
immutable, shared modifiers in initializers, but I think that 
cases like this are important too. Should I file an issue or it 
already exists, or it doesn't matter, or etc?

Thanks for attention


More information about the Digitalmars-d mailing list