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


Groups > comp.lang.java.programmer > #33200 > unrolled thread

Generic cannot be applied

Started byPhilipp Kraus <philipp.kraus.flashpixx@gmail.com>
First post2015-11-25 13:33 -0800
Last post2015-11-26 11:06 -0600
Articles 4 — 4 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  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

#33200 — Generic cannot be applied

FromPhilipp Kraus <philipp.kraus.flashpixx@gmail.com>
Date2015-11-25 13:33 -0800
SubjectGeneric cannot be applied
Message-ID<46618f03-00af-47fe-affa-5e12b9a2f4cc@googlegroups.com>
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>
{
      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 <<< ?

Thanks

Phil

[toc] | [next] | [standalone]


#33202

FromArne Vajhøj <arne@vajhoej.dk>
Date2015-11-25 16:53 -0500
Message-ID<n35ale$t6c$2@speranza.aioe.org>
In reply to#33200
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

[toc] | [prev] | [next] | [standalone]


#33203

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2015-11-25 17:00 -0500
Message-ID<n35atn$332$1@dont-email.me>
In reply to#33200
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

[toc] | [prev] | [next] | [standalone]


#33208

FromJoshua Cranmer 🐧 <Pidgeot18@verizon.invalid>
Date2015-11-26 11:06 -0600
Message-ID<n37e32$tsm$1@dont-email.me>
In reply to#33200
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web