Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #38190
| From | Daniele Futtorovic <da.futt.news@laposte.net> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Lambda dereference |
| Date | 2018-03-17 03:10 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <p8htfh$p2r$1@dont-email.me> (permalink) |
| References | <p8h2a1$4f0$1@dont-email.me> <p8h2lu$773$1@dont-email.me> <slrnpaolja.5mg.avl@logic.at> |
On 2018-03-17 00:42, Andreas Leitgeb wrote:
> Daniele Futtorovic <da.futt.news@laposte.net> wrote:
>> On 2018-03-16 19:26, Daniele Futtorovic wrote:
>>> interface Tagged {
>>> boolean tag();
>>> }
>>> Stream<Tagged> stream = ...;
>>> You can do this (exhibit A):
>>> Predicate<Tagged> p = Tagged::tag;
>>> Collection<Tagged> untagged = stream.filter( p.negate()
>>> ).collect(Collectors.toSet());
>>> But you can't do this (exhibit B):
>>> Collection<Tagged> untagged = stream.filter( (Tagged::tag).negate()
>>> ).collect(Collectors.toSet());
>>> Why not? It would be pretty neat. Or am I missing something?
>>
>> This works:
>> Collection<Tagged> untagged = stream.filter(
>> ((Predicate<Tagged>)Tagged::tag).negate() ).collect(Collectors.toSet());
>>
>> ... and it makes sense why it wouldn't accept it without the casting.
>
> Huh, why? Well, Java does have some "expressions" that only work as
> initializers (like braced list of elements for initializing arrays), but
> that wouldn't look to me like such a case - afterall one could use
> Tagged::tag right there if it weren't for the ".negate()".
>
> What would be the expression type of blank (Tagged::tag)?
>
No, absolutely -- the compiler can't determine that Tagged::tag is a
Predicate without the context, and hence (Tagged::tag).negate() will
fail (parentheses just for clarification). I'd figured as much, but then
when I tried to add a cast, I didn't seem to be going anywhere. So I
thought, hey, that's something I could post to cljp! And when it was
posted, I thought, just a second, I'd better double-check some more.
And, oh fortuna, thou art a bitch, the cast worked.
Foot.values().forEach( Mouth.get()::insert ); :-/
--
DF.
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Find similar | Unroll thread
Lambda dereference Daniele Futtorovic <da.futt.news@laposte.net> - 2018-03-16 19:26 +0100
Re: Lambda dereference Daniele Futtorovic <da.futt.news@laposte.net> - 2018-03-16 19:32 +0100
Re: Lambda dereference Andreas Leitgeb <avl@logic.at> - 2018-03-16 23:42 +0000
Re: Lambda dereference Daniele Futtorovic <da.futt.news@laposte.net> - 2018-03-17 03:10 +0100
csiph-web