[ 
https://issues.apache.org/jira/browse/WICKET-2137?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12678768#action_12678768
 ] 

Olivier Michallat commented on WICKET-2137:
-------------------------------------------

Hi, I'm new to the mailing list where I followed the debate about this issue.

IMO, using a wildcard generic type doesn't "widen" the API, it only locks the 
caller code into using the wildcard type (I'm attaching a Java class 
illustrating what I'll describe below).

Let's say I want to construct a DropDownChoice<Number>. The current API tells 
me that I must pass an IModel<List<? extends Number>>.
Since Integer extends Number, it should be OK to pass a model that returns a 
list of Integers: IModel<List<Integer>>.
But it's not.
For the generic type system, an IModel<List<Integer>> is NOT an IModel<List<? 
extends Number>>
Nor is an IModel<List<Number>> for that matter (as illustrated by Brill's 
initial post).

My only workaround is to declare my model as an instance of the wildcard type. 
I can return my list of Integers as a covariant return:

  IModel<List<? extends Number>> model = new LoadableDetachableModel<List<? 
extends Number>>() {
    protected List<Integer> load() { return Arrays.asList(1, 2, 3); }
  };
  new DropDownChoice<Number>("ddc", model);

I have two issues with that:
* "if the user of a class has to think about wildcard types, there is probably 
something wrong with the class's API" (the Gospel according to St Joshua, 
second edition, item 28)
* since I'm passing a wildcard type to DropDownChoice, I'm not actually telling 
anything about the fact that my list contains Integers.

So +1 for changing DropDownChoice to match ListView.
In the example above, it means using either an IModel<List<Number>> or a 
DropDownChoice<Integer>


On a side note:
There would be a way to make the constructor of DropDownChoice<Number> accept 
an IModel<List<Integer>>: generifying the constructor itself
  <U extends T> DropDownChoice(String id, IModel<List<U>> model)

But that only moves the problem further down, as type param U is local to the 
constructor and cannot be used elsewhere in the class. I've also thought of 
passing Class<U> as a type token, but that's runtime information: it can't be 
used by generics (and we certainly don't want to go into reflection). 

> Remove ? extends from constructor of DropDownChoice
> ---------------------------------------------------
>
>                 Key: WICKET-2137
>                 URL: https://issues.apache.org/jira/browse/WICKET-2137
>             Project: Wicket
>          Issue Type: Improvement
>          Components: wicket
>    Affects Versions: 1.4-RC3
>         Environment: 1.4-SNAPSHOT as of 2009-01-28
>            Reporter: Brill Pappin
>
> DropDownChoice has a generics definition of List<? extends T> in its 
> constructor. 
> This causes trouble with existing models that may be used in a DropDownChoice 
> and is not consistant with ListView which uses <List<T>>.
> Change DropDownChoice to match the other list type components so that their 
> models can be used across components.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply via email to