"it"? "#"? ""?
Matthew Adams
matthew at matthewadams.me
Fri Nov 18 15:47:10 PST 2011
NB: I'm searching through the archives on this and didn't see anything
that directly addressed it.
I just got through the slides at
http://blogs.oracle.com/briangoetz/entry/slides_from_devoxx_talk_on
and noticed a nice feature inspired by Groovy that was missing from the
slide code examples. I don't know if it's missing from the lambda
proposal, though -- I can't tell from the slides.
Groovy defaults the name of a single closure argument to "it". I think
this would be nice to have in JDK8 lambdas, too.
=====
// Without "it":
void expire(File root, long before) {
...
root.listFiles(File p -> p.lastModified() <= before);
...
}
=====
// With "it":
void expire(File root, long before) {
...
root.listFiles(it.lastModified() <= before);
...
}
======
Is this possible to include, or will the grammar require "->" so that
"it.lastModified <= before" isn't interpreted by the compiler as a boolean
expression? If that's the case, how about considering "#" (or some other
appropriate character) instead of "it"? That way, the compiler would know
implicitly that if it encounters a "#", it *must* be a lambda expression
taking a single variable of an inferred type:
=====
// With "#":
void expire(File root, long before) {
...
root.listFiles(#.lastModified() <= before);
...
}
======
You could even reduce "it" or "#" to an empty string and just use the "."
with no preceding scope. I don't know if the grammar could support it, but
it's interesting. I'm not sure I like it, but is sure is compact!
=====
// With "":
void expire(File root, long before) {
...
root.listFiles(.lastModified() <= before);
...
}
======
Another example:
=====
// explicit lambda param name
Set<Album> favs = albums
.filter(a -> a.tracks.anyMatch(t -> (t.rating >= 4)))
.into(new HashSet<>());
=====
// "it"
Set<Album> favs = albums
.filter(it.tracks.anyMatch(it.rating >= 4)) // 2 its!?!?
.into(new HashSet<>());
=====
// "#"
Set<Album> favs = albums
.filter(#.tracks.anyMatch(#.rating >= 4))
.into(new HashSet<>());
=====
// ""
Set<Album> favs = albums
.filter(.tracks.anyMatch(.rating >= 4))
.into(new HashSet<>());
=====
Thoughts?
-matthew
--
@matthewadams12
mailto:matthew at matthewadams.me
skype:matthewadams12
yahoo:matthewadams
aol:matthewadams12
google-talk:matthewadams12 at gmail.com
msn:matthew at matthewadams.me
http://matthewadams.me
http://www.linkedin.com/in/matthewadams
More information about the lambda-dev
mailing list