"it"? "#"? ""?

Brian Goetz brian.goetz at oracle.com
Mon Nov 21 07:39:20 PST 2011


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>  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>:
>>>>
>>>>> 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