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


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

Re: Generic cannot be applied

From Eric Sosman <esosman@comcast-dot-net.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: Generic cannot be applied
Date 2015-11-25 17:00 -0500
Organization A noiseless patient Spider
Message-ID <n35atn$332$1@dont-email.me> (permalink)
References <46618f03-00af-47fe-affa-5e12b9a2f4cc@googlegroups.com>

Show all headers | View raw


On 11/25/2015 4:33 PM, Philipp Kraus wrote:
> Hello,
>
> 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>

     Did you mean IOperator?

> {
>        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 new Double 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 <<< ?

     I certainly can't!  I use generics by following patterns
without understanding, so I'm not much of an explainer.  ;-)

     But I can suggest a cure: Interfaces as before, then

	public class myOperator implements IOperator<Number> {
	    public IVariable<Number> evaluate(IVariable<Number> p) {
	        p.set(p.get().doubleValue() + 1);
	        return p;
	    }
	}

-- 
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM

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