java - Impossible typing when an argument accepts Collection<X<?>> -


this problem comes typing of constructor of javax.validation.constraintviolationexception. accepts set<constraintviolation<?>> argument.

while it's easy set of constraintviolation<x> x concrete type, seems impossible set of "constraintviolation<?>" well-typed api. , not possible convert former latter without using convoluted casts. (casting set<? extends constraintviolation<?>> , set<constraintviolation<?>>.)

so guys think api wrong or wrong (and why)?

the api wrong. unless implementations need add new constraintviolation<?>s set, should accept set<? extends constraintviolation<?>>.

here's example demonstrating why more flexible (provided paul bellora, thanks):

public class main {      interface foo<t> { }      interface subfoo<t> extends foo<t> { }      static class bar { }      public static void main(string[] args) {          set<foo<?>> arg1 = null;         set<subfoo<?>> arg2 = null;         set<foo<bar>> arg3 = null;         set<subfoo<bar>> arg4 = null;          set<foo<?>> inflexibleparam;         inflexibleparam = arg1; //success         inflexibleparam = arg2; //incompatible types         inflexibleparam = arg3; //incompatible types         inflexibleparam = arg4; //incompatible types          set<? extends foo<?>> flexibleparam;         flexibleparam = arg1; //success         flexibleparam = arg2; //success         flexibleparam = arg3; //success         flexibleparam = arg4; //success     } } 

(ideone)


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -