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


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

Re: Generic cannot be applied

From Arne Vajhøj <arne@vajhoej.dk>
Newsgroups comp.lang.java.programmer
Subject Re: Generic cannot be applied
Date 2015-11-25 16:53 -0500
Organization Aioe.org NNTP Server
Message-ID <n35ale$t6c$2@speranza.aioe.org> (permalink)
References <46618f03-00af-47fe-affa-5e12b9a2f4cc@googlegroups.com>

Show all headers | View raw


On 11/25/2015 4:33 PM, Philipp Kraus wrote:
> I have got these interface structure
>
> public interface IVariable<T>
> {
>
>       public T get();
>       public void set(T v);
> }
>
> public interface IOperator<T>
> {
>        public IVariable<T> evaluate( IVariable<T> p );
> }
>
>
> and I implement this class
>
> public class myOperator<T extends Number> implements Operator<T>
> {
>        public IVariable<T> evaluate( IVariable<T> p )
>        {
>             p.set( new Double(p.get().doubleValue() + 1) );  <<<
>             return p;
>        }
> }
>
> The line with <<< create an compiler error and I need to cast the
> newDouble to T, but I don't understand, why I must cast. The error
 > message is "IVariable<T> cannot be applied". IMHO a Double inherit
 > from Number and T extends a Number on myOperator class, so an explicit
 > cast is not needed.
>
> Can anybody explain, why I need a cast at the point <<< ?

You have a type safe variable container that can encapsulate any
type that extends Number.

You can not set a Double because the method could have
been called with an IVariable<Integer>.

Arne

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next 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