You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Niranjan Rao <nh...@gmail.com> on 2011/07/28 19:48:36 UTC

What does this syntax say?

Ok, I admit it - I don't understand this function at all defined in 
IComponentInheritedModel

public <W> IWrapModel<W> wrapOnInheritance(Component component)

I don't understand meaning of <W> and IWrapModel<W>. I know generics 
generally, but this syntax has been baffling me. Based on what eclipse 
is trying to do, it seems like it will return IWrapModel<W>, but then 
what does first <W> do? I tried some google searches, but could not find 
the answer.

Thanks,

Niranjan

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: What does this syntax say?

Posted by Dan Retzlaff <dr...@gmail.com>.
I actually meant the no-argument version of "of()". Since this is getting
off-topic, I suggest you search around under "java type erasure." There are
people far more expert than I to describe what's going on. :)

On Thu, Jul 28, 2011 at 9:26 PM, Ben Tilford <be...@tilford.info> wrote:

> Right but Model.of accepts an instance of the generic type so it's not lost
> and is available at runtime.
>
> static Model<T> of(T instance)
> vs.
> public <W> IWrapModel<W> wrapOnInheritance(Component component)
>
> On Thu, Jul 28, 2011 at 6:33 PM, Dan Retzlaff <dr...@gmail.com> wrote:
>
> > Generic types are lost by the time the method is executed, so there's
> > really
> > nothing the method implementation could check. Another fun example
> > is org.apache.wicket.model.Model#of(). The general subject is called type
> > erasure, and is one of the more confusing aspects of Java generics.
> >
> > On Thu, Jul 28, 2011 at 4:45 PM, Ben Tilford <be...@tilford.info> wrote:
> >
> > > Without a Class argument how is it returning/casting correctly?
> Shouldn't
> > > it
> > > be
> > >
> > > public <W> IWrapModel<W> wrapOnInheritance(Component component,Class<W>
> > > type)
> > >
> > > to make W available within the method?
> > >
> > >
> > > On Thu, Jul 28, 2011 at 12:40 PM, Dan Retzlaff <dr...@gmail.com>
> > > wrote:
> > >
> > > > The first <W> let's the compiler know that the second <W> is a
> generic
> > > type
> > > > and not a reference to some class named W. It's just syntax.
> > > >
> > > > On Thu, Jul 28, 2011 at 10:48 AM, Niranjan Rao <nh...@gmail.com>
> > wrote:
> > > >
> > > > > Ok, I admit it - I don't understand this function at all defined in
> > > > > IComponentInheritedModel
> > > > >
> > > > > public <W> IWrapModel<W> wrapOnInheritance(Component component)
> > > > >
> > > > > I don't understand meaning of <W> and IWrapModel<W>. I know
> generics
> > > > > generally, but this syntax has been baffling me. Based on what
> > eclipse
> > > is
> > > > > trying to do, it seems like it will return IWrapModel<W>, but then
> > what
> > > > does
> > > > > first <W> do? I tried some google searches, but could not find the
> > > > answer.
> > > > >
> > > > > Thanks,
> > > > >
> > > > > Niranjan
> > > > >
> > > > >
> > >
> ------------------------------**------------------------------**---------
> > > > > To unsubscribe, e-mail: users-unsubscribe@wicket.**apache.org<
> > > > users-unsubscribe@wicket.apache.org>
> > > > > For additional commands, e-mail: users-help@wicket.apache.org
> > > > >
> > > > >
> > > >
> > >
> >
>

Re: What does this syntax say?

Posted by Ben Tilford <be...@tilford.info>.
Right but Model.of accepts an instance of the generic type so it's not lost
and is available at runtime.

static Model<T> of(T instance)
vs.
public <W> IWrapModel<W> wrapOnInheritance(Component component)

On Thu, Jul 28, 2011 at 6:33 PM, Dan Retzlaff <dr...@gmail.com> wrote:

> Generic types are lost by the time the method is executed, so there's
> really
> nothing the method implementation could check. Another fun example
> is org.apache.wicket.model.Model#of(). The general subject is called type
> erasure, and is one of the more confusing aspects of Java generics.
>
> On Thu, Jul 28, 2011 at 4:45 PM, Ben Tilford <be...@tilford.info> wrote:
>
> > Without a Class argument how is it returning/casting correctly? Shouldn't
> > it
> > be
> >
> > public <W> IWrapModel<W> wrapOnInheritance(Component component,Class<W>
> > type)
> >
> > to make W available within the method?
> >
> >
> > On Thu, Jul 28, 2011 at 12:40 PM, Dan Retzlaff <dr...@gmail.com>
> > wrote:
> >
> > > The first <W> let's the compiler know that the second <W> is a generic
> > type
> > > and not a reference to some class named W. It's just syntax.
> > >
> > > On Thu, Jul 28, 2011 at 10:48 AM, Niranjan Rao <nh...@gmail.com>
> wrote:
> > >
> > > > Ok, I admit it - I don't understand this function at all defined in
> > > > IComponentInheritedModel
> > > >
> > > > public <W> IWrapModel<W> wrapOnInheritance(Component component)
> > > >
> > > > I don't understand meaning of <W> and IWrapModel<W>. I know generics
> > > > generally, but this syntax has been baffling me. Based on what
> eclipse
> > is
> > > > trying to do, it seems like it will return IWrapModel<W>, but then
> what
> > > does
> > > > first <W> do? I tried some google searches, but could not find the
> > > answer.
> > > >
> > > > Thanks,
> > > >
> > > > Niranjan
> > > >
> > > >
> > ------------------------------**------------------------------**---------
> > > > To unsubscribe, e-mail: users-unsubscribe@wicket.**apache.org<
> > > users-unsubscribe@wicket.apache.org>
> > > > For additional commands, e-mail: users-help@wicket.apache.org
> > > >
> > > >
> > >
> >
>

Re: What does this syntax say?

Posted by Dan Retzlaff <dr...@gmail.com>.
Generic types are lost by the time the method is executed, so there's really
nothing the method implementation could check. Another fun example
is org.apache.wicket.model.Model#of(). The general subject is called type
erasure, and is one of the more confusing aspects of Java generics.

On Thu, Jul 28, 2011 at 4:45 PM, Ben Tilford <be...@tilford.info> wrote:

> Without a Class argument how is it returning/casting correctly? Shouldn't
> it
> be
>
> public <W> IWrapModel<W> wrapOnInheritance(Component component,Class<W>
> type)
>
> to make W available within the method?
>
>
> On Thu, Jul 28, 2011 at 12:40 PM, Dan Retzlaff <dr...@gmail.com>
> wrote:
>
> > The first <W> let's the compiler know that the second <W> is a generic
> type
> > and not a reference to some class named W. It's just syntax.
> >
> > On Thu, Jul 28, 2011 at 10:48 AM, Niranjan Rao <nh...@gmail.com> wrote:
> >
> > > Ok, I admit it - I don't understand this function at all defined in
> > > IComponentInheritedModel
> > >
> > > public <W> IWrapModel<W> wrapOnInheritance(Component component)
> > >
> > > I don't understand meaning of <W> and IWrapModel<W>. I know generics
> > > generally, but this syntax has been baffling me. Based on what eclipse
> is
> > > trying to do, it seems like it will return IWrapModel<W>, but then what
> > does
> > > first <W> do? I tried some google searches, but could not find the
> > answer.
> > >
> > > Thanks,
> > >
> > > Niranjan
> > >
> > >
> ------------------------------**------------------------------**---------
> > > To unsubscribe, e-mail: users-unsubscribe@wicket.**apache.org<
> > users-unsubscribe@wicket.apache.org>
> > > For additional commands, e-mail: users-help@wicket.apache.org
> > >
> > >
> >
>

Re: What does this syntax say?

Posted by Scott Swank <sc...@gmail.com>.
This is called a "generic method", and you're just giving the type
signature of the method. Here's an example from our code.

	public static <T extends Number & Comparable<T>> NumberField<T>
withMinimum(String id, T min) {
		NumberField<T> f = new NumberField<T>(id);
		f.add(new MinimumValidator<T>(min));
		return f;
	}

Which allows:

NumberField<Integer> adultAge = NumberField.withMinimum("bar", 18);
NumberField<Float> rating = NumberField.withinRange("foo", 0.0, 5.0);

Scott





On Thu, Jul 28, 2011 at 11:55 PM, Wilhelmsen Tor Iver
<To...@arrive.no> wrote:
>> public <W> IWrapModel<W> wrapOnInheritance(Component component,Class<W>
>> type)
>
> The Class<W> parameter is only needed if you intend to do "new W();" or the like in the method (the Class is then something the compiler can grab hold of for calling the constructor). For just passing the type parameter to other generic classes it is not needed. The compiler sees the context (i.e. the left-hand side of an assignment) and if it cannot determine the type it will say "needs a cast" as a warning, but the code will compile since it's all Object anyway.
>
> - Tor Iver
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


RE: What does this syntax say?

Posted by Wilhelmsen Tor Iver <To...@arrive.no>.
> public <W> IWrapModel<W> wrapOnInheritance(Component component,Class<W>
> type)

The Class<W> parameter is only needed if you intend to do "new W();" or the like in the method (the Class is then something the compiler can grab hold of for calling the constructor). For just passing the type parameter to other generic classes it is not needed. The compiler sees the context (i.e. the left-hand side of an assignment) and if it cannot determine the type it will say "needs a cast" as a warning, but the code will compile since it's all Object anyway. 

- Tor Iver

Re: What does this syntax say?

Posted by Ben Tilford <be...@tilford.info>.
Without a Class argument how is it returning/casting correctly? Shouldn't it
be

public <W> IWrapModel<W> wrapOnInheritance(Component component,Class<W>
type)

to make W available within the method?


On Thu, Jul 28, 2011 at 12:40 PM, Dan Retzlaff <dr...@gmail.com> wrote:

> The first <W> let's the compiler know that the second <W> is a generic type
> and not a reference to some class named W. It's just syntax.
>
> On Thu, Jul 28, 2011 at 10:48 AM, Niranjan Rao <nh...@gmail.com> wrote:
>
> > Ok, I admit it - I don't understand this function at all defined in
> > IComponentInheritedModel
> >
> > public <W> IWrapModel<W> wrapOnInheritance(Component component)
> >
> > I don't understand meaning of <W> and IWrapModel<W>. I know generics
> > generally, but this syntax has been baffling me. Based on what eclipse is
> > trying to do, it seems like it will return IWrapModel<W>, but then what
> does
> > first <W> do? I tried some google searches, but could not find the
> answer.
> >
> > Thanks,
> >
> > Niranjan
> >
> > ------------------------------**------------------------------**---------
> > To unsubscribe, e-mail: users-unsubscribe@wicket.**apache.org<
> users-unsubscribe@wicket.apache.org>
> > For additional commands, e-mail: users-help@wicket.apache.org
> >
> >
>

Re: What does this syntax say?

Posted by Dan Retzlaff <dr...@gmail.com>.
The first <W> let's the compiler know that the second <W> is a generic type
and not a reference to some class named W. It's just syntax.

On Thu, Jul 28, 2011 at 10:48 AM, Niranjan Rao <nh...@gmail.com> wrote:

> Ok, I admit it - I don't understand this function at all defined in
> IComponentInheritedModel
>
> public <W> IWrapModel<W> wrapOnInheritance(Component component)
>
> I don't understand meaning of <W> and IWrapModel<W>. I know generics
> generally, but this syntax has been baffling me. Based on what eclipse is
> trying to do, it seems like it will return IWrapModel<W>, but then what does
> first <W> do? I tried some google searches, but could not find the answer.
>
> Thanks,
>
> Niranjan
>
> ------------------------------**------------------------------**---------
> To unsubscribe, e-mail: users-unsubscribe@wicket.**apache.org<us...@wicket.apache.org>
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

Re: What does this syntax say?

Posted by Bas Gooren <ba...@iswd.nl>.
This syntax is for use when you need a generic placeholder.

In this case, it means that <W> is determined by the call site:

IModel<BusinessObject> model = OtherModel.wrapOnInheritance( Component );

The above means that W is checked to be "BusinessObject" for all 
occurrences of W.

A better to understand example is to have a look at 
java.util.Collections, eg: addAll 
<http://download.oracle.com/javase/6/docs/api/java/util/Collections.html#addAll%28java.util.Collection,%20T...%29>
It's signature is:

static <T> boolean addAll( Collection<? super T> c, T... elements);

Which means: you can only add elements of the generic type constraining 
the collection (e.g. only add String elements to a Collection<String>).

Hope this helps!

Bas

Op 28-7-2011 19:48, schreef Niranjan Rao:
> Ok, I admit it - I don't understand this function at all defined in 
> IComponentInheritedModel
>
> public <W> IWrapModel<W> wrapOnInheritance(Component component)
>
> I don't understand meaning of <W> and IWrapModel<W>. I know generics 
> generally, but this syntax has been baffling me. Based on what eclipse 
> is trying to do, it seems like it will return IWrapModel<W>, but then 
> what does first <W> do? I tried some google searches, but could not 
> find the answer.
>
> Thanks,
>
> Niranjan
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>