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


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

Re: assign lambda to functional interface

Path csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From Eric Sosman <esosman@comcast-dot-net.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: assign lambda to functional interface
Date Sat, 11 May 2019 09:38:09 -0400
Organization A noiseless patient Spider
Lines 39
Message-ID <qb6j84$acf$1@dont-email.me> (permalink)
References <6594e2c1-ec44-4cbc-b6fb-2c8a852fa7dd@googlegroups.com> <qb689k$f0s$1@dont-email.me> <4a576120-782d-4519-bee1-14f1c85c9125@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 7bit
Injection-Date Sat, 11 May 2019 13:38:13 -0000 (UTC)
Injection-Info reader02.eternal-september.org; posting-host="92b24b19ec7ae61651c16d3d18ef595d"; logging-data="10639"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18t3ntOx5KZWd+o4qQZb7wY"
User-Agent Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1
Cancel-Lock sha1:76Hd5WQCfVYRliG1MQmwyM1fA7k=
In-Reply-To <4a576120-782d-4519-bee1-14f1c85c9125@googlegroups.com>
Content-Language en-US
Xref csiph.com comp.lang.java.programmer:38960

Show key headers only | View raw


On 5/11/2019 8:57 AM, gk wrote:
> On Saturday, May 11, 2019 at 4:01:26 PM UTC+5:30, Eric Sosman wrote:
>> On 5/11/2019 12:58 AM, gk wrote:
>>> Can we assign a expression into an interface like this ?
>>> [... code snipped ...]
>>
>>       Yes.  Did something go wrong when you tried it?
>>
>> -- 
>> esosman@comcast-dot-net.invalid
>> Six hundred twenty-one days to go.
> 
> This works . But that is a weird looking syntax. .... an expression going into interface which was not there in jdk 1.6 and below.

     Is it "weird" that newer versions of Java add capabilities
not present before?

> also , can we do the same assignment to this to consumer also ?
> 
> Consumer c =  i -> (i < 18);  // Consumer is functional interface.

     ... but its accept() method is void, so a boolean-valued
expression can't implement it.  A different lambda could work.

> In fact can we assign this to any interface as well (non functional interface) ?

     You can use a lambda expression as an instance of any functional
interface, because an F.I. has only one method and thus Java can
figure out which method the lambda implements.

     You can use a lambda for a multi-method interface provided the
interface has exactly one abstract method and all the others have
`default' implementations.  You can use a lambda for Consumer's
accept() method (because add() has a `default' version), but you
couldn't use a lambda for List.

-- 
esosman@comcast-dot-net.invalid
Six hundred twenty-one days to go.

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


Thread

assign lambda to functional interface gk <srcjnu@gmail.com> - 2019-05-10 21:58 -0700
  Re: assign lambda to functional interface Eric Sosman <esosman@comcast-dot-net.invalid> - 2019-05-11 06:31 -0400
    Re: assign lambda to functional interface gk <srcjnu@gmail.com> - 2019-05-11 05:57 -0700
      Re: assign lambda to functional interface Eric Sosman <esosman@comcast-dot-net.invalid> - 2019-05-11 09:38 -0400
  Re: assign lambda to functional interface gk <srcjnu@gmail.com> - 2019-05-12 06:28 -0700
    Re: assign lambda to functional interface Eric Sosman <esosman@comcast-dot-net.invalid> - 2019-05-12 10:22 -0400

csiph-web