News for hackers. Stuff that matters.
Re:Lambda expression comparison between C++11, C++14 and C++17 by hendzen on Saturday May 14, 2016 @06:07PM (#11698273)
How is it ugly? The capture list is a necessary complexity in a language with manual memory management.
Re:Lambda expression comparison between C++11, C++14 and C++17 by steveklabnik on Saturday May 14, 2016 @06:13PM (#11698292)
It's a trade off more than a necessity. For example, Rust doesn't have explicit capture lists, and if you want explicit control, you make new bindings and capture those. You almost never need to do this in Rust, so it's optimized for that case; I haven't written many closures in C++, so I can't say as much about the frequency there.

To make this more concrete:

    let s = String::from("s");
    
    let closure = || {
        println!("s is: {}", s);
    };
    
    closure();
If you wanted to capture s in a different way:

    let s = String::from("s");
    
    let s1 = &s;
    let closure = || {
        println!("s1 is: {}", s1);
    };
    
    closure();
No capture list needed, same control.
Re:Lambda expression comparison between C++11, C++14 and C++17 by kibwen on Saturday May 14, 2016 @06:18PM (#11698308)
Rust doesn't have explicit capture lists, but it does have the `move` modifier on closures which is like C++'s `[=]`.

Strictly speaking, Rust probably could have gotten away with having neither the `move` modifier nor capture clauses at all, but it would have had wide-ranging implications on the ergonomics and capabilty of closures.

Re:Lambda expression comparison between C++11, C++14 and C++17 by hendzen on Saturday May 14, 2016 @06:20PM (#11698317)
How would you do the equivalent of this in Rust?

  auto on_heap = std::make_unique<MyType>(...);

  function_that_accepts_lambda([obj = std::move(on_heap)]() {
     obj->bar(...);
  })
Re:Lambda expression comparison between C++11, C++14 and C++17 by steveklabnik on Saturday May 14, 2016 @06:22PM (#11698323)

    let on_heap = Box::new(...);

    function_that_accepts_lambda(move || {
        on_heap.bar();
    });
This is sort of what kibwen was mentioning: move is a single annotation that overrides everything to capture by value rather than have it inferred.
Re:Lambda expression comparison between C++11, C++14 and C++17 by hendzen on Saturday May 14, 2016 @06:39PM (#11698383)
What if you want to move some things, but copy others?

e.g.

  auto shared = std::make_shared<MySharedType>(...);
  auto unique = std::make_unique<MyOwnedType>(...);

  function_that_accepts_lambda([shared, u = std::move(unique)] {
      shared->foo(...); u->bar(...);
  });

  // Outer scope can still use shared.
  shared->foo(....);
Re:Lambda expression comparison between C++11, C++14 and C++17 by steveklabnik on Saturday May 14, 2016 @07:03PM (#11698461)
I am 99% sure this is identical:

    let on_heap = Box::new(...);
    let shared = Arc::new(...);

    let s = shared.clone();
    function_that_accepts_lambda(move || {
        on_heap.bar();
        s.foo();
    });
We have to make the extra s binding.

Also, my sibling is correct that Copy types will just be copied, not moved.

Re:Lambda expression comparison between C++11, C++14 and C++17 by vvanders on Saturday May 14, 2016 @06:46PM (#11698403)
If the type implements Copy they'll be implicitly copied when moved into the Rust closure(I think). Or you can declare a scope var and clone() manually.
Re:Lambda expression comparison between C++11, C++14 and C++17 by partisan on Saturday May 14, 2016 @06:15PM (#11698297)
The syntax is not the prettiest, but it is legible once you understand what [](){} means.

In C#, there is no such thing, but there is a part of me that wishes we had such a thing. I like the ability explicitly state what variables are being captured.

Re:Lambda expression comparison between C++11, C++14 and C++17 by masklinn on Sunday May 15, 2016 @03:47AM (#11699948)
> I like the ability explicitly state what variables are being captured.

Why? You state what variables are being captured by just using them in the lambda body.

Re:Lambda expression comparison between C++11, C++14 and C++17 by jlebar on Sunday May 15, 2016 @05:23AM (#11700131)
> You state what variables are being captured by just using them in the lambda body.

Wow, have you never spent a week debugging a JavaScript memory leak?

Re:Lambda expression comparison between C++11, C++14 and C++17 by masklinn on Sunday May 15, 2016 @08:05AM (#11700602)
No. What I have done on the other hand was add unused lexical variables to an anonymous function so the runtime wouldn't optimise them out of the closure and I could still see them in the debugger.
Re:Lambda expression comparison between C++11, C++14 and C++17 by rtpg on Saturday May 14, 2016 @09:00PM (#11698812)
I'm not 100% sure, but C#'s compiler should automatically capture what you need (and leave out the rest).

I think the primary need for manual declaration is because in C++ you need to differentiate between pass by copy semantics and pass by reference semantics.

Re:Lambda expression comparison between C++11, C++14 and C++17 by masklinn on Sunday May 15, 2016 @03:58AM (#11699981)
> I think the primary need for manual declaration is because in C++ you need to differentiate between pass by copy semantics and pass by reference semantics.

That's not actually a need, C++ includes [=] and [&] (capture everything by value or by reference). You can get a mix by creating references outside the body then capturing the environment by value (capturing the references by value and thus getting references).

On the one hand it has a bit more syntactic overhead (you have to take and declare a bunch of references before creating the closure), on the other hand there's less irregularity to the language, and bindings mean the same thing in and out of the closure.

FWIW that's what Rust does[0], though it may help that Rust's blocks are "statement expressions", some constructs would probably be unwieldy without that.

[0] the default corresponds to C++'s [&] (capture by ref), and a "move closure" switches to [=] instead

Re:Lambda expression comparison between C++11, C++14 and C++17 by noblethrasher on Saturday May 14, 2016 @09:27PM (#11698902)
Yep, in Microsoft’s C# compiler, only the closed over local variables of a function are captured (which, in C#’s case, means generating a class with fields corresponding to each closed over local, and then replacing those locals with references to their respective fields of an instance of that class).
Re:Lambda expression comparison between C++11, C++14 and C++17 by spdionis on Sunday May 15, 2016 @08:14AM (#11700625)
PHP also doesn't capture variables by default (except $this) and i like it that way.
Re:Lambda expression comparison between C++11, C++14 and C++17 by vanderZwan on Saturday May 14, 2016 @07:31PM (#11698539)
The only thing I find somewhat frustrating about the syntax is that the notation messes with my existing expectations. Up until now, in C-like languages a [] was just for collections and indexing into them, in the code I used at least.

I mean I'm not really complaining; I don't see better syntax to fit short anonymous functions into the existing syntax, without defeating the whole purpose of it either.

I suspect it's just a matter of getting used to this extra meaning for angular brackets.

Re:Lambda expression comparison between C++11, C++14 and C++17 by kibwen on Saturday May 14, 2016 @06:15PM (#11698298)
Very often the capture list is empty, it could have been elided (as the parameter list can be) if the syntax could have been made unambiguous.
Re:Lambda expression comparison between C++11, C++14 and C++17 by yongjik on Saturday May 14, 2016 @06:19PM (#11698314)
Well, you need something to indicate the beginning of a lambda. So you can think of "[]" as serving that role, instead of "lambda" in Python or "\" in Haskell.
Re:Lambda expression comparison between C++11, C++14 and C++17 by rubber_duck on Saturday May 14, 2016 @06:52PM (#11698423)
You can use C# fat arrow syntax, it even allows removing the braces for single expression lambdas which is the most common from anyway.
Re:Lambda expression comparison between C++11, C++14 and C++17 by vanderZwan on Saturday May 14, 2016 @08:17PM (#11698682)
> removing the braces

If I have learned anything over the years, it's that removing the braces anywhere is the introduction of a bug during a rewrite waiting to happen.

Re:Lambda expression comparison between C++11, C++14 and C++17 by rubber_duck on Sunday May 15, 2016 @04:32PM (#11702602)
It really works without any issues in C# from my experience.

Statemends like :

    list.Where(e => e.Property == ExpectedValue).Select(e => e.Property)
is much cleaner than something like :

    list.Where([](auto e) { return e.Property == ExpectedValue; }).Select([](auto e) { return e.Property; });
Braces and capture declaration adds 0 value here and it's ~80% of the use cases I see for lambdas.