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


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

Re: Generic cannot be applied

Path csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail
From Eric Sosman <esosman@comcast-dot-net.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: Generic cannot be applied
Date Wed, 25 Nov 2015 17:00:33 -0500
Organization A noiseless patient Spider
Lines 51
Message-ID <n35atn$332$1@dont-email.me> (permalink)
References <46618f03-00af-47fe-affa-5e12b9a2f4cc@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=windows-1252; format=flowed
Content-Transfer-Encoding 7bit
Injection-Date Wed, 25 Nov 2015 21:58:15 -0000 (UTC)
Injection-Info mx02.eternal-september.org; posting-host="858a9d65d95f4ea4d355bd116aa2cdbf"; logging-data="3170"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+o5Qs6DlgR/eoavJ+pwzMn"
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.3.0
In-Reply-To <46618f03-00af-47fe-affa-5e12b9a2f4cc@googlegroups.com>
Cancel-Lock sha1:sdcUEMFy3QGL/tRBRmB/liXXgFg=
Xref csiph.com comp.lang.java.programmer:33203

Show key headers only | 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