First, remember that a .NET String
is both IConvertible
and ICloneable
.
Now, consider the following quite simple code:
<
I believe the compiler does the better thing in VB.NET with the warning, but I still don't think that is going far enough. Unfortunately, the "right thing" probably either requires disallowing something that is potentially useful(implementing the same interface with two covariant or contravariant generic type parameters) or introducing something new to the language.
As it stands, there is no place the compiler could assign an error right now other than the HungryWolf
class. That is the point at which a class is claiming to know how to do something that could potentially be ambiguous. It is stating
I know how to eat an
ICloneable
, or anything implementing or inheriting from it, in a certain way.And, I also know how to eat an
IConvertible
, or anything implementing or inheriting from it, in a certain way.
However, it never states what it should do if it receives on its plate something that is both an ICloneable
and an IConvertible
. This doesn't cause the compiler any grief if it is given an instance of HungryWolf
, since it can say with certainty "Hey, I don't know what to do here!". But it will give the compiler grief when it is given the ICanEat
instance. The compiler has no idea what the actual type of the object in the variable is, only that it definitely does implement ICanEat
.
Unfortunately, when a HungryWolf
is stored in that variable, it ambiguously implements that exact same interface twice. So surely, we cannot throw an error trying to call ICanEat
, as that method exists and would be perfectly valid for many other objects which could be placed into the ICanEat
variable (batwad already mentioned this in one of his answers).
Further, although the compiler could complain that the assignment of a HungryWolf
object to an ICanEat
variable is ambiguous, it cannot prevent it from happening in two steps. A HungryWolf
can be assigned to an ICanEat
variable, which could be passed around into other methods and eventually assigned into an ICanEat
variable. Both of these are perfectly legal assignments and it would be impossible for the compiler to complain about either one.
Thus, option one is to disallow the HungryWolf
class from implementing both ICanEat
and ICanEat
when ICanEat
's generic type parameter is contravariant, since these two interfaces could unify. However, this removes the ability to code something useful with no alternative workaround.
Option two, unfortunately, would require a change to the compiler, both the IL and the CLR. It would allow the HungryWolf
class to implement both interfaces, but it would also require the implementation of the interface ICanEat
interface, where the generic type parameter implements both interfaces. This likely is not the best syntax(what does the signature of this Eat(T)
method look like, Eat(IConvertible & ICloneable food)
?). Likely, a better solution would be to an auto-generated generic type upon the implementing class so that the class definition would be something like:
class HungryWolf:
ICanEat,
ICanEat,
ICanEat
where TGenerated_ICloneable_IConvertible: IConvertible, ICloneable {
// implementation
}
The IL would then have to changed, to be able to allow interface implementation types to be constructed just like generic classes for a callvirt
instruction:
.class auto ansi nested private beforefieldinit HungryWolf
extends
[mscorlib]System.Object
implements
class NamespaceOfApp.Program/ICanEat`1,
class NamespaceOfApp.Program/ICanEat`1,
class NamespaceOfApp.Program/ICanEat`1)!TGenerated_ICloneable_IConvertible>
The CLR would then have to process callvirt
instructions by constructing an interface implementation for HungryWolf
with string
as the generic type parameter for TGenerated_ICloneable_IConvertible
, and checking to see if it matches better than the other interface implementations.
For covariance, all of this would be simpler, since the extra interfaces required to be implemented wouldn't have to be generic type parameters with constraints but simply the most derivative base type between the two other types, which is known at compile time.
If the same interface is implemented more than twice, then the number of extra interfaces required to be implemented grows exponentially, but this would be the cost of the flexibility and type-safety of implementing multiple contravariant(or covariant) on a single class.
I doubt this will make it into the framework, but it would be my preferred solution, especially since the new language complexity would always be self-contained to the class which wishes to do what is currently dangerous.
edit:
Thanks Jeppe for reminding me that covariance is no simpler than contravariance, due to the fact that common interfaces must also be taken into account. In the case of string
and char[]
, the set of greatest commonalities would be {object
, ICloneable
, IEnumerable
} (IEnumerable
is covered by IEnumerable
).
However, this would require a new syntax for the interface generic type parameter constraint, to indicate that the generic type parameter only needs to
Possibly something like:
interface ICanReturn where T: class {
}
class ReturnStringsOrCharArrays:
ICanReturn,
ICanReturn,
ICanReturn
where TGenerated_String_ArrayOfChar: object|ICloneable|IEnumerable {
}
The generic type parameter TGenerated_String_ArrayOfChar
in this case(where one or more interfaces are common) always have to be treated as object
, even though the common base class has already derived from object
; because the common type may implement a common interface without inheriting from the common base class.