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


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

Re: Generic cannot be applied

From Joshua Cranmer 🐧 <Pidgeot18@verizon.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: Generic cannot be applied
Date 2015-11-26 11:06 -0600
Organization A noiseless patient Spider
Message-ID <n37e32$tsm$1@dont-email.me> (permalink)
References <46618f03-00af-47fe-affa-5e12b9a2f4cc@googlegroups.com>

Show all headers | 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