"it"? "#"? ""?

Rémi Forax forax at univ-mlv.fr
Fri Nov 18 15:12:35 PST 2011


Matthew,
there is a special list for discussing about lambdas:
   lambda-dev at openjdk.java.net
that is better for this kind of proposal.

Also you can take a look to the archive, or ask google to dig in it,
   http://mail.openjdk.java.net/pipermail/lambda-dev/
there is a good chance that there is a thread about 'it'.

Rémi

On 11/18/2011 10:25 PM, Matthew Adams wrote:
> 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



More information about the jdk8-dev mailing list