Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #7836

Re: new Java lambda syntax

From BGB <cr88192@hotmail.com>
Newsgroups comp.lang.java.programmer
Subject Re: new Java lambda syntax
Date 2011-09-11 16:18 -0700
Organization albasani.net
Message-ID <j4jfkq$upl$1@news.albasani.net> (permalink)
References (3 earlier) <alpine.DEB.2.00.1109111255530.13018@urchin.earth.li> <9j3vj8-aqe.ln1@news.simpsonst.f2s.com> <j4item$o69$1@news.albasani.net> <tsdvj8-vbg.ln1@news.simpsonst.f2s.com> <j4jbgt$i60$1@dont-email.me>

Show all headers | View raw


On 9/11/2011 3:07 PM, Joshua Cranmer wrote:
> On 9/11/2011 2:14 PM, Steven Simpson wrote:
>> On 11/09/11 19:08, BGB wrote:
>>> I would have also liked to see lexical variable capture.
>>> FFS, I added this (along with closures) to a C compiler before, can't
>>> be too hard
>>
>> At this stage, I don't think the issue is how, but whether/when to
>> permit it.
>
> There are other issues like does it capture the value or does it use the
> same variable. e.g., what would this produce:
>
> List<Runnable> runners = new LinkedList<Runnable>();
> for (int i = 0; i < 10; i++) {
> runners.add(() => { System.out.println("Value of i is " + i); });
> }
> for (Runnable r : runners) {
> r.run();
> }
>
> Should you see 0..9 or 10 repeated 10 times?
>

most languages I am aware of with closures (and mutable state) capture 
the variable itself, so one would see 10 repreated 10 times (since the 
original variable now holds 10).


in a different context, I had run into this issue, and added a special 
form to the block (theoretically, IIRC not yet implemented) to 
explicitly capture the state of the variable at that point (rather than 
a reference to this variable). interestingly, this internally converted 
into a closure which accepted the variables as arguments and was then 
called with these variables.

a more generalized form of this would look something like:
for(i=0; i<10; i++)
     begin(i) {
         ...
}
...

with "begin(i) { ... }" basically meaning to execute '...' with 'i' 
having been captured (by value).

this could also be user like "begin(i, j) {...}" to capture two values, 
or "begin(i, j=i*251) {...}" to capture the value of i and bind j as a 
computed value (sort of like "(let)" and friends in Lisp and Scheme).

however, I have doubts that such a feature would map nearly so cleanly 
to Java or the JVM.


in C++0x, the type of variable capture was made explicit in the lambda 
syntax:
"[](...) {...}" (no capture allowed)
vs
"[&](...) {...}" (capture by reference)
vs
"[=](...) {...}" (capture by value).
vs
more complex forms...



>> For invocations, having to type obj.run() instead of obj() is hardly
>> onerous. Plus, invocations will be much rarer than lambda declarations.
>> Also note that the invocation site is unaware of whether the object is a
>> lambda.
>
> Also, note the (slight) benefits of explicitly saying what you are
> doing. You might choose, reasonably, to call the callback parameter for
> an asyncForEach function `block', at which point the functional call
> specification becomes block(value), which can be visually ambiguous as
> to what it's doing. block.call(value) is clearer, on the other hand.
>

yes, but I guess it depends some on what one is doing, and whether or 
not it is better to complicate some potential use cases for sake of 
preventing people from shooting themselves in the foot in others (or, 
OTOH, gloss over certain complexities at the risk of people then 
shooting themselves in the foot...).


or such...

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

new Java lambda syntax markspace <-@.> - 2011-09-08 13:19 -0700
  Re: new Java lambda syntax Roedy Green <see_website@mindprod.com.invalid> - 2011-09-08 15:18 -0700
    Re: new Java lambda syntax Arne Vajhøj <arne@vajhoej.dk> - 2011-09-08 18:27 -0400
    Re: new Java lambda syntax BGB <cr88192@hotmail.com> - 2011-09-08 15:40 -0700
      Re: new Java lambda syntax Arne Vajhøj <arne@vajhoej.dk> - 2011-09-08 19:27 -0400
        Re: new Java lambda syntax BGB <cr88192@hotmail.com> - 2011-09-08 16:29 -0700
          Re: new Java lambda syntax Arne Vajhøj <arne@vajhoej.dk> - 2011-09-08 19:48 -0400
          Re: new Java lambda syntax Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2011-09-08 17:56 -0700
            Re: new Java lambda syntax BGB <cr88192@hotmail.com> - 2011-09-08 22:23 -0700
    Re: new Java lambda syntax "Nasser M. Abbasi" <nma@12000.org> - 2011-09-08 16:41 -0700
    Re: new Java lambda syntax Joshua Cranmer <Pidgeot18@verizon.invalid> - 2011-09-08 20:50 -0500
  Re: new Java lambda syntax bugbear <bugbear@trim_papermule.co.uk_trim> - 2011-09-09 09:33 +0100
  Re: new Java lambda syntax Tom Anderson <twic@urchin.earth.li> - 2011-09-10 14:48 +0100
    Re: new Java lambda syntax Steven Simpson <ss@domain.invalid> - 2011-09-10 16:17 +0100
      Re: new Java lambda syntax Tom Anderson <twic@urchin.earth.li> - 2011-09-11 13:06 +0100
        Re: new Java lambda syntax Steven Simpson <ss@domain.invalid> - 2011-09-11 17:18 +0100
          Re: new Java lambda syntax BGB <cr88192@hotmail.com> - 2011-09-11 11:08 -0700
            Re: new Java lambda syntax Steven Simpson <ss@domain.invalid> - 2011-09-11 20:14 +0100
              Re: new Java lambda syntax BGB <cr88192@hotmail.com> - 2011-09-11 14:08 -0700
              Re: new Java lambda syntax Joshua Cranmer <Pidgeot18@verizon.invalid> - 2011-09-11 17:07 -0500
                Re: new Java lambda syntax BGB <cr88192@hotmail.com> - 2011-09-11 16:18 -0700
                Re: new Java lambda syntax Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2011-09-13 02:00 +0200
                Re: new Java lambda syntax Arne Vajhøj <arne@vajhoej.dk> - 2011-09-12 20:59 -0400
                Re: new Java lambda syntax Joshua Cranmer <Pidgeot18@verizon.invalid> - 2011-09-12 21:24 -0500
                Re: new Java lambda syntax Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2011-09-13 21:04 +0200
                Re: new Java lambda syntax Joshua Cranmer <Pidgeot18@verizon.invalid> - 2011-09-13 15:22 -0500
                Re: new Java lambda syntax Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2011-09-13 23:25 +0200
                Re: new Java lambda syntax Tom Anderson <twic@urchin.earth.li> - 2011-09-13 21:29 +0100
                Re: new Java lambda syntax Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2011-09-13 23:26 +0200
                Re: new Java lambda syntax supercalifragilisticexpialadiamaticonormalizeringelimatisticantations <supercalifragilisticexpialadiamaticonormalizeringelimatisticantations@averylongandannoyingdomainname.com> - 2011-09-13 20:25 -0400
                Re: new Java lambda syntax Tom Anderson <twic@urchin.earth.li> - 2011-09-15 21:58 +0100
                Re: new Java lambda syntax Joshua Cranmer <Pidgeot18@verizon.invalid> - 2011-09-13 21:36 -0500
                Re: new Java lambda syntax supercalifragilisticexpialadiamaticonormalizeringelimatisticantations <supercalifragilisticexpialadiamaticonormalizeringelimatisticantations@averylongandannoyingdomainname.com> - 2011-09-13 22:49 -0400
                Re: new Java lambda syntax "supercalifragilisticexpialadiamaticonormalizeringelimatisticantations" <supercalifragilisticexpialadiamaticonormalizeringelimatisticantations@averylongandannoyingdomainname.com> - 2011-09-14 02:49 -0400
                Re: new Java lambda syntax supercalifragilisticexpialadiamaticonormalizeringelimatisticantations <supercalifragilisticexpialadiamaticonormalizeringelimatisticantations@averylongandannoyingdomainname.com> - 2011-09-14 03:01 -0400
                Re: new Java lambda syntax "winkleMeister" <..00@00.00.00.1> - 2011-09-14 09:59 +0000
                Re: new Java lambda syntax supercalifragilisticexpialadiamaticonormalizeringelimatisticantations <supercalifragilisticexpialadiamaticonormalizeringelimatisticantations@averylongandannoyingdomainname.com> - 2011-09-15 10:16 -0400
                Re: new Java lambda syntax supercalifragilisticexpialadiamaticonormalizeringelimatisticantations <supercalifragilisticexpialadiamaticonormalizeringelimatisticantations@averylongandannoyingdomainname.com> - 2011-09-14 06:40 -0400
                Re: new Java lambda syntax supercalifragilisticexpialadiamaticonormalizeringelimatisticantations <supercalifragilisticexpialadiamaticonormalizeringelimatisticantations@averylongandannoyingdomainname.com> - 2011-09-15 10:16 -0400
                Re: new Java lambda syntax lightworker <etts@0n.org.null> - 2011-09-16 01:57 +0000
                Re: new Java lambda syntax thoolen <th00len@th0lenbot.thorium> - 2011-09-15 22:41 -0400
                Re: new Java lambda syntax Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-09-14 06:29 -0300
                Re: new Java lambda syntax BGB <cr88192@hotmail.com> - 2011-09-14 07:40 -0700
                Re: new Java lambda syntax Lew <lewbloch@gmail.com> - 2011-09-14 08:01 -0700
                Re: new Java lambda syntax BGB <cr88192@hotmail.com> - 2011-09-14 14:50 -0700
                Re: new Java lambda syntax Lew <lewbloch@gmail.com> - 2011-09-14 18:02 -0700
                Re: new Java lambda syntax BGB <cr88192@hotmail.com> - 2011-09-14 21:08 -0700

csiph-web