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


I have recently found that you can specify several types in a type parameter bound (see example). Like any new device, I am trying to find out the possibility of how it can be used (and abuse) for example I have prepared this example.

On the sample given below, the compiler is giving me an error

Sending (new alphabet soup);

Method reminder (demo soup) is unclear for the demo

I understand it because the method signing the match My question is how can this be sorted out without changing the methods? If I want to force the soup version to be called, then I can reduce the soup:

Sending (soup) new alphabet soup ())

I'm unsure how you will apply calls to another version. Is this possible?

  public class demo {interface HasA {public char getA (); } Interface HasB {public char getB (); } Interface is {Public Care (CAC); } Interface soup {public zero-free (); } Class alphabet implies HasA, HasB, HasC {public char getA () {return 'a'; } Public letter find (b) (return 'b';) public four mill (cc) (return 'c';}} class alphabet soup, soup, smile, hasb, hsiisi {public zero-free ()) {System.out Public print (public) (return to 'b'; public 'four' (CC) (return 'b';) C ';}} Public Zero Dispatch (Soup Soup) {System.out.println ("Something is eating soup ..."); soup.eat ();} Public & lt; T Hasa & HasB & amp; ; HasC> zero transmission (s) Letter) {System.out.println ("Reading ABC ..."); Println (letters.getA ()); Println (letters.getB ()); Println (letters.getC ());} Public Zero Test ( } {Send (New Alphabet ()); Remittance (New Alphabet Soup);} Public Static Zero Main (String [] Args) {New Demo (). Test ();}}  

- Edit: Just got to know that "many bound type parameters are formally referred to as" Insensection Type "

Note that the error is not related to the generic, if you use the interface and there is a type of intersection, you get the same result:

  Public class AA {Interface XX} interface YY {}; Public Xerox (XX x) {} Public Nullification Some (YY X) {} class XY XX implementation, YY {} Public Zero Runner () some (new XY));}}  

If you receive the same error in "doSomething", the compiler can not solve the opacity. Do you want to interpret XX or YY? an artist. But if you have a hierarchy, like "YY XX" and "XY implies YY", then the compiler can guess the correct method to call.


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -