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


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

Re: Java 8 Lambda with generic types

Newsgroups comp.lang.java.programmer
Date 2016-08-28 00:02 -0700
References <df44fc1a-9d48-4ba8-890e-a687d24062a9@googlegroups.com>
Message-ID <cf527430-e940-4ba7-9579-096d1c80bd84@googlegroups.com> (permalink)
Subject Re: Java 8 Lambda with generic types
From Stone Zhong <stone.zhong@gmail.com>

Show all headers | View raw


On Saturday, August 27, 2016 at 11:50:40 PM UTC-7, Stone Zhong wrote:
> Hi there, 
> 
> It seems sometimes, the compiler is not smart enough to decide the generic parameter type based on labmda argument types, here is an example:
> 
> I have a interface, say Toy:
> 
> public interface Toy<E> {
>     List<E> get();
> }
> 
> 
> Now I have a method to process toys, such as:
>     public static void checkToy(Toy<?> toy) {
>     }
> 
> Now, the following code works
>         checkToy(new Toy<Integer>() {
>             @Override
>             public List<Integer> get() {
>                 List<Integer> ret = Arrays.asList(10);
>                 return ret;
>             }
>         });
> 
> However, if I have 
>         checkToy(() -> {
>             return Arrays.<Integer>asList(10);
>         });
> 
> I got compiling error: "List<Integer> cannot be converted to List<Object>", it seems compiler tried to create a instance of Toy<Object> instead of Toy<Integer> based on the lambda. Is there a wait to tell the compiler it is Toy<Integer> explicitly with the lambda expression?
> 
> Thanks,
> Stone

I just learned if I put all generic parameter in the method parameter even if you do not use it, it helps the compiler to find the right generic parameter type when instantiate the instance. for example:

public interface Toy<E> {
    List<E> get(E dummy);
}

did kind of solve the problem, but I do feel bit clumsy doing such thing.

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Java 8 Lambda with generic types Stone Zhong <stone.zhong@gmail.com> - 2016-08-27 23:50 -0700
  Re: Java 8 Lambda with generic types Stone Zhong <stone.zhong@gmail.com> - 2016-08-28 00:02 -0700
    Re: Java 8 Lambda with generic types Arne Vajhøj <arne@vajhoej.dk> - 2016-08-28 07:46 -0400
      Re: Java 8 Lambda with generic types Steven Simpson <ss@my.domain.invalid> - 2016-08-28 15:00 +0100
        Re: Java 8 Lambda with generic types Lew <lewbloch@gmail.com> - 2016-08-30 22:25 -0700
          Re: Java 8 Lambda with generic types Steven Simpson <ss@my.domain.invalid> - 2016-08-31 17:19 +0100
  Re: Java 8 Lambda with generic types Steven Simpson <ss@my.domain.invalid> - 2016-08-28 08:19 +0100
  Re: Java 8 Lambda with generic types Stone Zhong <stone.zhong@gmail.com> - 2016-08-28 03:26 -0700
  Re: Java 8 Lambda with generic types Stone Zhong <stone.zhong@gmail.com> - 2016-08-28 07:45 -0700
    Re: Java 8 Lambda with generic types Arne Vajhøj <arne@vajhoej.dk> - 2016-08-28 13:22 -0400
  Re: Java 8 Lambda with generic types Stone Zhong <stone.zhong@gmail.com> - 2016-08-28 07:57 -0700
  Re: Java 8 Lambda with generic types Stone Zhong <stone.zhong@gmail.com> - 2016-08-28 10:43 -0700
    Re: Java 8 Lambda with generic types Arne Vajhøj <arne@vajhoej.dk> - 2016-08-30 20:35 -0400
  Re: Java 8 Lambda with generic types Stone Zhong <stone.zhong@gmail.com> - 2016-09-02 15:18 -0700

csiph-web