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


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

Re: Java 8 Lambda with generic types

Path csiph.com!news.swapon.de!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From Steven Simpson <ss@my.domain.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: Java 8 Lambda with generic types
Date Sun, 28 Aug 2016 08:19:26 +0100
Organization A noiseless patient Spider
Lines 39
Message-ID <usfa9d-k75.ln1@s.simpson148.btinternet.com> (permalink)
References <df44fc1a-9d48-4ba8-890e-a687d24062a9@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 7bit
Injection-Info mx02.eternal-september.org; posting-host="aeef5e178076fda4998e4379bb1adbab"; logging-data="23304"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18n1cHEGaD447ysOBbvIhNt8SrqE/iYieg="
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.2.0
In-Reply-To <df44fc1a-9d48-4ba8-890e-a687d24062a9@googlegroups.com>
Cancel-Lock sha1:m8upiglZolN33tUTsxZC9at8dD0=
Xref csiph.com comp.lang.java.programmer:35484

Show key headers only | View raw


On 28/08/16 07:50, Stone Zhong wrote:

> 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?

Do you need the <Integer> qualification on asList?  It seems to work 
without:

import java.util.*;
import java.util.function.*;

public class Funk {
     interface Toy<E> {
         List<E> get();
     }
     
     static void checkToy(Toy<?> toy) { }

     public void test() {
         checkToy(new Toy<Integer>() {
                 @Override
                 public List<Integer> get() {
                     return Arrays.asList(10);
                 }
             });

         checkToy(() -> Arrays.asList(10));
         checkToy(() -> { return Arrays.asList(10); });
     }
}


-- 
ss at comp dot lancs dot ac dot uk

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