On Sat, Feb 12, 2011 at 7:56 AM, Willis Blackburn <wbo...@panix.com> wrote:

>
> I'm constantly running into problems with this method:
>
> public static <C> IModel<List<? extends C>> ofList(final List<? extends C>
> list)
>
> This says that ofList takes, as a parameter, a List of C or some subclass
> of
> C, and returns a List of C or of some subclass of C.
>
> The problem that I keep having is that the type of the input list and the
> type of the output list may not be the same.  I think that in practice they
> are, but the compiler does not know that.  If C = StringedInstrument then
> the parameter may be List<Violin> while the returned list may be
> List<Guitar>.
>
> So every time I call Model.listOf, the type parameter of my List becomes
> hazy, which has annoying downstream effects.  If I load a
> List<StringedInstrument> from a database, then pass it to Model.ofList, I
> wind up with a List<? extends StringedInstrument>--the compiler has no idea
> what the generic type of the List is, only that whatever it is, it is
> compatible with StringedInstrument.


As a side note, rarely should you ever use Model class for a list of things,
especially things loaded from a database.  If you then pass that model to a
component, all the things in it will be serialized.

You should be using LoadableDetachableModel and have the "load from
database" logic in the load method.

Model is just a dataholder implementation of IModel that should typically
only be used for transient data (data that can't be loaded from somewhere)
because it must serialize whatever you get it.  This is especially bad
whenever you are dealing with any collection of things.


>  I can still get StringedInstrument
> instances out of the List, but I can't pass it to any method (like a
> component constructor) that takes a List<StringedInstrument> or an
> IModel<List<StringedInstrument>> without ugly casting.
>
> Since Model.ofList must return a list that is compatible with the original
> list--it's a wrapper, not a transformer--can't it just take List<C> and
> return List<C>?
>
> Same goes for the other ofCollection-type methods.


So use the Model constructors instead.  The factory methods are just there
to help remove some verbosity related to generics.

-- 
Jeremy Thomerson
http://wickettraining.com
*Need a CMS for Wicket?  Use Brix! http://brixcms.org*

Reply via email to