469,330 Members | 1,399 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,330 developers. It's quick & easy.

Overload ambiguous when it shouldn't be?

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.
Nov 21 '05 #1
2 1634

"Michi Henning" <mi***@zeroc.com> wrote in message
news:uC**************@TK2MSFTNGP09.phx.gbl...
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/de...bspec4_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.
Nov 21 '05 #2
"Imran Koradia" <no****@microsoft.com> wrote in message
news:uG***************@TK2MSFTNGP09.phx.gbl...

That is correct behaviour. Read this document:
http://msdn.microsoft.com/library/de...bspec4_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.

--
Michi Henning Ph: +61 4 1118-2700
ZeroC, Inc. http://www.zeroc.com

Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Gianni Mariani | last post: by
25 posts views Thread by Steve Richter | last post: by
reply views Thread by Michi Henning | last post: by
9 posts views Thread by Tony | last post: by
6 posts views Thread by Jonas Huckestein | last post: by
11 posts views Thread by onkar | last post: by
3 posts views Thread by i3x171um | last post: by
2 posts views Thread by xtrigger303 | last post: by
9 posts views Thread by sebastian | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.