www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Wrong overload resolution

reply "rumbu" <rumbu rumbu.ro> writes:
This problem appears only if one of the parameters is an 
interface. Without it or using any other type as a second 
parameter instead of the interface, it compiles. Also it compiles 
if the passed interface is null. The example below uses 
short/ushort, but I found the same behaviour for any combination 
of integral types of the same bitsize (byte/ubyte/char, 
ushort/short/wchar, int/uint/dchar, long/ulong)

D 2.066.1

interface I {}
class C: I {}

void func(ushort s, I i)
{
     writeln("ushort overload");
}

void func(short s, I i)
{
     writeln("short overload");
}

void call(short s)
{
     C c = new C();
     I d = new C();
     func(s, c); // ---- ERROR ---- see below

     //but these are ok

     func(s, cast(I)c) //ok
     func(s, d) //ok
     func(s, null) //ok

}

main.func called with argument types (short, C) matches both:	
main.func(short s, I i)	
main.func(ushort s, I i)	
Feb 15 2015
next sibling parent "Baz" <bb.temp gmx.com> writes:
On Sunday, 15 February 2015 at 23:48:50 UTC, rumbu wrote:
 This problem appears only if one of the parameters is an 
 interface. Without it or using any other type as a second 
 parameter instead of the interface, it compiles. Also it 
 compiles if the passed interface is null. The example below 
 uses short/ushort, but I found the same behaviour for any 
 combination of integral types of the same bitsize 
 (byte/ubyte/char, ushort/short/wchar, int/uint/dchar, 
 long/ulong)

 D 2.066.1

 interface I {}
 class C: I {}

 void func(ushort s, I i)
 {
     writeln("ushort overload");
 }

 void func(short s, I i)
 {
     writeln("short overload");
 }

 void call(short s)
 {
     C c = new C();
     I d = new C();
     func(s, c); // ---- ERROR ---- see below

     //but these are ok

     func(s, cast(I)c) //ok
     func(s, d) //ok
     func(s, null) //ok

 }

 main.func called with argument types (short, C) matches both:	
 main.func(short s, I i)	
 main.func(ushort s, I i)
it's intereting to note that if func() are rewritten: --- void func(ref ushort s, I i){} void func(ref short s, I i){} --- or even --- void func(const ref ushort s, I i){} void func(const ref short s, I i){} --- the problem doesn't happend.
Feb 15 2015
prev sibling parent "Meta" <jared771 gmail.com> writes:
On Sunday, 15 February 2015 at 23:48:50 UTC, rumbu wrote:
 This problem appears only if one of the parameters is an 
 interface. Without it or using any other type as a second 
 parameter instead of the interface, it compiles. Also it 
 compiles if the passed interface is null. The example below 
 uses short/ushort, but I found the same behaviour for any 
 combination of integral types of the same bitsize 
 (byte/ubyte/char, ushort/short/wchar, int/uint/dchar, 
 long/ulong)

 D 2.066.1

 interface I {}
 class C: I {}

 void func(ushort s, I i)
 {
     writeln("ushort overload");
 }

 void func(short s, I i)
 {
     writeln("short overload");
 }

 void call(short s)
 {
     C c = new C();
     I d = new C();
     func(s, c); // ---- ERROR ---- see below

     //but these are ok

     func(s, cast(I)c) //ok
     func(s, d) //ok
     func(s, null) //ok

 }

 main.func called with argument types (short, C) matches both:	
 main.func(short s, I i)	
 main.func(ushort s, I i)	
I have submitted a bug report here: https://issues.dlang.org/show_bug.cgi?id=14187
Feb 16 2015