« Return to Thread: generics in Wicket

RE: generics in Wicket

by Stefan Lindner :: Rate this Message:

Reply to Author | View in Thread

So the component developert must be very disciplined because the local variable "model" ist not generic. You can assign whatever IModel you want to the local "model". And the "return this.model" line will cause a warning.
 
In most cases it makes sense to have Generic components.
 
Stefan Lindner

________________________________

Von: Ryan Holmes [mailto:ryan@...]
Gesendet: Di 20.03.2007 07:58
An: wicket-dev@...
Betreff: Re: generics in Wicket




On Mar 19, 2007, at 12:58 AM, Eelco Hillenius wrote:
>
> Do you have an idea that works without having to generify Component?
>

Not sure if you would put this in the "idea that works" category, but
I was thinking of something like this:

public class Component {
        private IModel model;
       
        public <T> Component(final String id, final IModel<T> model) {
                this.model = model;
        }
       
        public <T> IModel<T> getModel() {
                return this.model;
        }
       
        public <T> void setModel(IModel<T> model) {
                this.model = model;
        }
}

This accommodates generic models but doesn't enforce type consistency
between getModel and setModel. In practice, the types would usually
be inferred:

IModel<Integer> model = component.getModel();

so it's easy to use and does eliminate casting, but get/set model
inconsistency would only be caught at runtime (unless you use the
full generic method syntax with a type parameter)

I don't know -- it doesn't really buy you much. This doesn't work:

Integer i = component.getModel().getObject();

I also played around with the idea of a generic "ModelHolder" object
inside of Component so at least getModel() and setModel() would use
the same type parameter, but again the get/set model methods on
Component itself can't pick that type up.

<sigh> Oh well, maybe it's just taking me longer to come to the same
conclusion you did.

-Ryan



 « Return to Thread: generics in Wicket