How do I resolve ambiguous methods caused by intersection types in Java generics?

前端 未结 5 420
猫巷女王i
猫巷女王i 2020-12-31 16:52

I just recently discovered that you can specify multiple types in a single type parameter bound (see example). Like any new tool, I\'ve been trying to explore the possibilit

5条回答
  •  时光取名叫无心
    2020-12-31 17:22

    The compiler is right, and saves you from a mess.

    AlphaBetSoup is a subtype of soup and also a subtype of HasA, HasB, and HasC

    Therefore, it fits the bill for both versions of Dispatch

    Since Soup is not a subtype of HasA, HasB, or HasC, it also can't say that one version is more "specific" than the other.

    Therefore you'll get the error correctly.

    Overloaded method should not be ambiguous. If you have a type that mixes both types and you had an overload for each, change your hierarchy or get rid of an overload. It's wrong use of subtyping and overloading.

提交回复
热议问题