www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Implicit conversion to base interface doesn't work inside

reply "Uranuz" <neuranuz gmail.com> writes:
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
Jul 17 2014
parent reply "Uranuz" <neuranuz gmail.com> writes:
Actualy I found this bug report, but I don't know if there 
suggested something around associative arrays

https://issues.dlang.org/show_bug.cgi?id=5498
Jul 17 2014
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Uranuz:

 Actualy I found this bug report, but I don't know if there 
 suggested something around associative arrays

 https://issues.dlang.org/show_bug.cgi?id=5498
I think it's a bug. Issue 5498 it's better left closed. Search if another more specific bug is open on this, or open a new one otherwise: interface IBase {} class Impl(T): IBase { T value; } void main() { IBase x = new Impl!uint; IBase y = new Impl!string; IBase z = new Impl!double; IBase[string] fldFormats1 = [ "Key": x, "Name": y, "Percent": z ]; IBase[string] fldFormats2 = [ "Key": new Impl!uint, "Name": new Impl!string, "Percent": new Impl!double ]; } Kenji fixes two similar bugs before breakfast ;-) Bye, bearophile
Jul 17 2014
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Reduced:

interface IBase {}
class Impl(T): IBase {
     T value;
}
void main() {
     IBase a = true ? (new Impl!uint) : (new Impl!string);
}



test.d(6,23): Error: cannot implicitly convert expression (new 
Impl!uint) of type object.Object to test.IBase

Bye,
bearophile
Jul 17 2014
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
 interface IBase {}
 class Impl(T): IBase {
     T value;
 }
 void main() {
     IBase a = true ? (new Impl!uint) : (new Impl!string);
 }
I have added a note: https://issues.dlang.org/show_bug.cgi?id=3543 Bye, bearophile
Jul 17 2014
parent reply "Uranuz" <neuranuz gmail.com> writes:
On Thursday, 17 July 2014 at 17:15:54 UTC, bearophile wrote:
 interface IBase {}
 class Impl(T): IBase {
    T value;
 }
 void main() {
    IBase a = true ? (new Impl!uint) : (new Impl!string);
 }
I have added a note: https://issues.dlang.org/show_bug.cgi?id=3543 Bye, bearophile
OK. Thanks. Using casts everywhere for such case us quite annoying. I've tested it for plain array it doesn't work too. However issue is marked as solved for it.
Jul 17 2014
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Uranuz:

 OK. Thanks. Using casts everywhere for such case us quite 
 annoying. I've tested it for plain array it doesn't work too. 
 However issue is marked as solved for it.
It's not exactly the same issue. Issue 3543 seems more fitting. In D interfaces and classes are not the same thing. Bye, bearophile
Jul 17 2014
parent reply "Uranuz" <neuranuz gmail.com> writes:
On Thursday, 17 July 2014 at 17:27:37 UTC, bearophile wrote:
 Uranuz:

 OK. Thanks. Using casts everywhere for such case us quite 
 annoying. I've tested it for plain array it doesn't work too. 
 However issue is marked as solved for it.
It's not exactly the same issue. Issue 3543 seems more fitting. In D interfaces and classes are not the same thing. Bye, bearophile
OK. I'll try to use beta version of 2.066 until release happens
Jul 20 2014
parent "Uranuz" <neuranuz gmail.com> writes:
I posted in the wrong thread.
Jul 20 2014