Overload ambiguous when it shouldn't be?

M

Michi Henning

Hi,

the following code produces an error on the second-last line:

Interface Left
Sub op()
End Interface

Interface Right
Sub op(ByVal i As Integer)
End Interface

Public Interface LR
Inherits Left, Right
End Interface

Class Derived
Implements LR

Public Overloads Sub op() Implements Left.op
End Sub

Public Overloads Sub op(ByVal i As Integer) Implements Right.op
End Sub
End Class

Sub test()
Dim d As LR = New Derived
d.op() 'op' is ambiguous across the inherited interfaces 'Client.Left' and 'Client.Right'.
End Sub

I don't see what's ambiguous here. One operation has a parameter, and one does not have
a parameter, so there is no ambiguity. Interestingly, replacing the declaration of d with:

Dim d As Derived = New Derived

makes the error disappear. But I don't see why a call on a Derived would be
any less ambiguous than a call on an LR -- both have the same methods with the
same signatures.

Compiler bad?

Cheers,

Michi.
 
I

Imran Koradia

Michi Henning said:
Hi,

the following code produces an error on the second-last line:

Interface Left
Sub op()
End Interface

Interface Right
Sub op(ByVal i As Integer)
End Interface

Public Interface LR
Inherits Left, Right
End Interface

Class Derived
Implements LR

Public Overloads Sub op() Implements Left.op
End Sub

Public Overloads Sub op(ByVal i As Integer) Implements Right.op
End Sub
End Class

Sub test()
Dim d As LR = New Derived
d.op() 'op' is ambiguous across the inherited interfaces 'Client.Left'
and 'Client.Right'.
End Sub

That is correct behaviour. Read this document:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbls7/html/vblrfvbspec4_2_2.asp
According to this document:
"Unlike other types, which only derive from a single base type, an interface
may derive from multiple base interfaces. Because of this, an interface can
inherit an identically named type member from different base interfaces. In
such a case, the multiply inherited name is not available in the derived
interface, and referring to any of those type members through the derived
interface causes a compile-time error, regardless of signatures or
overloading. Instead, conflicting type members must be referenced through a
base interface name."

So, you should be doing something like:

Class Derived
Implements LR

' note that you dont even need the Overloads keyword
Public Sub op() Implements Left.op

End Sub

Public Sub op(ByVal i As Integer) Implements Right.op

End Sub
End Class

Sub Test()
Dim d As LR = New Derived

' access the op from Left
DirectCast(d, Left).op()

' access the op from Right
Dim i As Integer
DirectCast(d, Right).op(i)

End Sub


hope that helps..
Imran.
 
M

Michi Henning

Imran Koradia said:
That is correct behaviour. Read this document:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbls7/html/vblrfvbspec4_2_2.asp
According to this document:
"Unlike other types, which only derive from a single base type, an interface
may derive from multiple base interfaces. Because of this, an interface can
inherit an identically named type member from different base interfaces. In
such a case, the multiply inherited name is not available in the derived
interface, and referring to any of those type members through the derived
interface causes a compile-time error, regardless of signatures or
overloading. Instead, conflicting type members must be referenced through a
base interface name."

I agree -- the compiler behaves as the doc says it should.

I don't understand the motivation though. Why would overload
resolution not apply to interfaces, but apply to classes?
So, you should be doing something like:
[...]

Sub Test()
Dim d As LR = New Derived

' access the op from Left
DirectCast(d, Left).op()

' access the op from Right
Dim i As Integer
DirectCast(d, Right).op(i)

End Sub

Sure, the cast fixes it. But requiring the case appears
to be pointless. In particular, if I write:

Dim D As Derived = New Derived
d.op() ' No error here

then the compiler is quite content to apply overload resolution
and does exactly what you would expect.

So, why should it matter whether the reference is of class
type or interface type? Either way, the same set of method
candidates is available, so why apply overload resolution
in one case but not the other?

Cheers,

Michi.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top