"it"? "#"? ""?

Yuval Shavit yshavit at akiban.com
Mon Nov 21 07:53:00 PST 2011


I don't know how relevant it is to this discussion, but the amount of magic
that goes on in Scala is exactly what turned me off of it. There's a big
caveat in my statement, in that I've never actually used Scala (just read
through the O'Reilly book) -- so maybe it's super intuitive once you're
actually in that world. But it seemed to me that there's a high learning
curve in terms of figuring out all the automagical things that it helpfully
does for you (even though you could have easily done them yourself).

Something like root.listFiles(#lastModified() <= before) isn't super
magical, but I still find root.listFiles(f -> f.lastModified() <= before)
to be a bit clearer at the cost of not much verbosity.

On Mon, Nov 21, 2011 at 10:44 AM, Brian Goetz <brian.goetz at oracle.com>wrote:

> Except that's exactly what Scala does.
>
> On 11/21/2011 10:42 AM, Matthew Adams wrote:
> > I can't really predict the future response of developers using this
> > feature.  Your "(_,_)" example is something that I, personally, would
> > never expect a compiler to figure out due to the ambiguity of using the
> > same symbol in the same context.
> >
> > On Mon, Nov 21, 2011 at 9:39 AM, Brian Goetz <brian.goetz at oracle.com
> > <mailto:brian.goetz at oracle.com>> wrote:
> >
> >     We are not planning to add any wunderbar-like feature.  However,
> >     Remi has pointed out an annoying hole, in that _ is currently a
> >     valid identifier under JLS 7, and therefore for single-argument
> >     lambdas, you *could* say
> >
> >       _ -> _ + 1
> >
> >     and the compiler would accept it -- but not for the reason you think.
> >
> >     The likely follow-on outcome is that people will *think* we've
> >     adopted wunderbars, and then wonder why the "stupid" compiler won't
> >     accept things like
> >
> >       (_, _) -> _ + _
> >
> >     and probably bitch about "why did you special-case the
> >     single-argument wunderbar but not the general case".
> >
> >
> >
> >     On 11/21/2011 10:06 AM, Matthew Adams wrote:
> >
> >         Hi Remi,
> >
> >         I don't understand what you're trying to say.  Are you saying
> >         that instead
> >         of "it", "#" or empty string (implicit closure param), "_" could
> >         be used as
> >         an implicit closure param?  If so, then Brian seems to have shot
> >         that down
> >         in his last email (no Scala wunderbars).  If not, please explain
> >         further.
> >
> >         -matthew
> >
> >         On Sat, Nov 19, 2011 at 3:26 PM, Rémi Forax<forax at univ-mlv.fr
> >         <mailto:forax at univ-mlv.fr>>  wrote:
> >
> >             On 11/19/2011 09:45 PM, Brian Goetz wrote:
> >
> >                 We're pretty satisfied with the degree of syntax
> >                 reduction we've
> >
> >             achieved so far.  You can make things arbitrarily compact,
> >             but that's not
> >             the goal.  I don't think that horizontal span is our biggest
> >             problem any
> >             more.  So don't expect any Scala-style wunderbars or
> >             Groovy-style it.
> >
> >             and don't forget that even if
> >
> >                root.listFiles(it.__lastModified()<= before);
> >
> >
> >             is not legal, this snippet is legal because '_' is a legal
> >             identifier
> >
> >                root.listFiles(_ ->   _.lastModified()<= before);
> >
> >
> >             Rémi
> >
> >
> >
> >
> >
> >                 On Nov 19, 2011, at 3:10 PM, John Nilsson wrote:
> >
> >                     Given that the usecase for this is about implicit
> >                     context i really like
> >
> >             the
> >
> >                     last option of just leaving the space before the
> >                     period unfilled.
> >
> >                     Similarly it would be nice if it worked for all
> >                     operators:
> >
> >             list.filter(>2)
> >
> >
> >                     BR,
> >                     John million
> >                     Den 19 nov 2011 00:51 skrev "Matthew
> >                     Adams"<matthew at matthewadams.me
> >                     <mailto:matthew at matthewadams.me>__>:
> >
> >                         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
> >                         <
> 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
> >                         <mailto:matthew at matthewadams.me>
> >                         skype:matthewadams12
> >                         yahoo:matthewadams
> >                         aol:matthewadams12
> >                         google-talk:matthewadams12 at __gmail.com
> >                         <mailto:google-talk%3Amatthewadams12 at gmail.com>
> >                         msn:matthew at matthewadams.me
> >                         <mailto:msn%3Amatthew at matthewadams.me>
> >                         http://matthewadams.me
> >                         http://www.linkedin.com/in/__matthewadams
> >                         <http://www.linkedin.com/in/matthewadams>
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> > --
> > @matthewadams12
> > mailto:matthew at matthewadams.me <mailto:matthew at matthewadams.me>
> > skype:matthewadams12
> > yahoo:matthewadams
> > aol:matthewadams12
> > google-talk:matthewadams12 at gmail.com
> > <mailto:google-talk%3Amatthewadams12 at gmail.com>
> > msn:matthew at matthewadams.me <mailto:msn%3Amatthew at matthewadams.me>
> > http://matthewadams.me
> > http://www.linkedin.com/in/matthewadams
> >
>
>


More information about the lambda-dev mailing list