-2

The code says it all:

interface a { }
interface ab : a { }
interface ac : a { }
interface igeneric<T> { }

//this is OK
class Clazz : igeneric<ab>, igeneric<ac>
{
}

//compiler error : 'Clazz2<T>' cannot implement both '<ac>' and 'igeneric<T>' because they may unify for some type parameter substitutions
class Clazz2<T> : igeneric<T>, igeneric<ac> where T : ab
{ 
}

Any ideas? Workarounds?

Qantas 94 Heavy
  • 14,790
  • 31
  • 61
  • 78
Yoni Shalom
  • 489
  • 4
  • 10
  • The inheritance hierarchy has no bearing on this: remove `a` entirely and note you get the same error. – AakashM Jun 21 '11 at 10:20

1 Answers1

3

Yes, a few ideas:

  • You certainly cannot do what you are trying to do. Compiler has made it clear
  • You certainly should not need to do what you are trying to do. Generic is a type-less inheritance of behaviour. Implementing 2 type-full generic interfaces of the same kind does not make any sense.
  • You have not presented the original problem you are trying to solve. If you do, we might be able to help you with the design.

UPDATE

They are not of the same kind, one is g and the other is g. think about IMessageHandler and IMessageHandler, in a class implementing both Handle(MessageA) and Handle(MessageB)

In the real world, you would not do that. Also it can lead to numerous problems. Let's say we have:

interface IFactory<T>
{
   T Create();
}

Then if I implement two:

class DontDoIt : IFactory<A>, IFactory<B>
{
    A Create();
    B Create(); // won't compile
}

This will not compile since the methods are different only in return type. Now you can perhaps overcome it by implementing the interfaces explicitly, but as I said it just does not make sense from the design principles.

Aliostad
  • 76,981
  • 19
  • 152
  • 203
  • They are not of the same kind, one is g and the other is g. think about IMessageHandler and IMessageHandler, in a class implementing both Handle(MessageA) and Handle(MessageB) – Yoni Shalom Jun 21 '11 at 10:09