Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #33208

Re: Generic cannot be applied

Path csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail
From Joshua Cranmer 🐧 <Pidgeot18@verizon.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: Generic cannot be applied
Date Thu, 26 Nov 2015 11:06:54 -0600
Organization A noiseless patient Spider
Lines 29
Message-ID <n37e32$tsm$1@dont-email.me> (permalink)
References <46618f03-00af-47fe-affa-5e12b9a2f4cc@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 7bit
Injection-Date Thu, 26 Nov 2015 17:04:35 -0000 (UTC)
Injection-Info mx02.eternal-september.org; posting-host="fd083e583875d719dcabb768f03a1877"; logging-data="30614"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/jsHi0BwWp3GRQyZYxPp6zjwi6MNz2dAs="
User-Agent Mozilla/5.0 (Windows NT 6.3; WOW64; rv:42.0) Gecko/20100101 Thunderbird/42.0
In-Reply-To <46618f03-00af-47fe-affa-5e12b9a2f4cc@googlegroups.com>
Cancel-Lock sha1:26g4XBz2EFKNWrw36S512LJdeyI=
Xref csiph.com comp.lang.java.programmer:33208

Show key headers only | View raw


On 11/25/2015 3:33 PM, Philipp Kraus wrote:
> public class myOperator<T extends Number> implements Operator<T>
> {
>        public IVariable<T> evaluate( IVariable<T> p )
>        {
>             p.set( new Double(p.get().doubleValue() + 1) );  <<<

The generic types don't mean what you think they mean.

It's perhaps easiest to understand the generic type subtyping 
relationships by illustrating how they work on a basic container type, 
e.g., List<T>.

A List<T> naturally stores objects of only type T. So if you have 
List<Number>, you can add Number objects to the list and you can get 
Number objects from the list. However, if you change to List<T extends 
Number>, what you're saying is that "This is a list of something. That 
something must be a subtype of Number." Thus, objects of type List<T 
extends Number> can't *store* anything (since you don't know what type 
it's storing), but you can read from the list.

In contrast, there is another type bound you can specify. List<T super 
Number> means "This is a list of something which is a superclass of 
Number"--so you can store Numbers in it, but you can't read anything 
from it (except as Objects).

-- 
Beware of bugs in the above code; I have only proved it correct, not 
tried it. -- Donald E. Knuth

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

Generic cannot be applied Philipp Kraus <philipp.kraus.flashpixx@gmail.com> - 2015-11-25 13:33 -0800
  Re: Generic cannot be applied Arne VajhĂžj <arne@vajhoej.dk> - 2015-11-25 16:53 -0500
  Re: Generic cannot be applied Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-11-25 17:00 -0500
  Re: Generic cannot be applied Joshua Cranmer 🐧 <Pidgeot18@verizon.invalid> - 2015-11-26 11:06 -0600

csiph-web