Steve M <st***@nospam.com> wrote:
Where does polymorphism give room for ambiguity at runtime?
Actually, it doesn't. And I don't believe my suggestion for a more dynamic
overloading mechanism does either. If the compiler will balk at ambiguity in
its currently implementation, it surely could do so in a dynamic one as
well.
How, without loss of utility and at the expense of simplicity?
Consider:
interface IFoo
{
}
interface IBar
{
}
class X : IFoo, IBar
{
}
class Y
{
void Something(object o)
{
Overloaded(o);
}
void Overloaded(object o)
{
}
void Overloaded(IFoo x)
{
}
void Overloaded(IBar y)
{
}
}
No problems at compile-time here - Overloaded(object o) is called by
Something whatever the actual type of the object referred to by the
value of o is. Now consider what would happen in the system you're
suggesting, with dynamic overloading. What happens when you pass in a
reference to an instance of X? Both Overloaded(IFoo x) and
Overloaded(IBar y) are better than Overloaded(Object o), but neither is
better than each other - so you have ambiguity. This can be seen at
compile-time in the current system if you try to change the parameter
of Something to X x instead of object o.
Now, the compiler could catch the potential ambiguity here, but that
makes the language even *more* complicated - and still for little gain.
It would also limit the usefulness of the virtual overloading in the
first place, as you'd never be able to specify disparate interfaces (or
a class and an interface that class doesn't implement) as overloaded
parameters for two different methods.
I stand by my view that the "cost" of this system in terms of
simplicity and efficiency outweighs the utility.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too