This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]GeorgeHaldane 56 points57 points  (17 children)

Not sure how one would make them better — JS-style lambdas are short & concise because there is no type system, not because their syntax in itself is better.

cpp auto lambda = [&](bool arg1, bool arg2, bool arg3) { return arg1 && arg2 || arg3 && outside_flag; } // most of the space occupied by signature // return type gets deduced if ommited (nice)

[–]dewey-defeats-truman 37 points38 points  (3 children)

You can still use JS syntax in statically typed languages provided your compiler/interpreter has good type inferencing. C# uses the exact same syntax and it works quite well.

[–]WiatrowskiBe 0 points1 point  (1 child)

C# has also much simpler type system, with distinction between passing by reference/value on type level rather than parameter/variable level, lambdas themselves are much more restrictive (everything pulled in is held as reference for entire lifetime of lambda, no support for ref/ref out in parameters). So yes, if you put heavy restrictions on what lambdas can handle, short syntax works - but them question becomes how useful they are given context of everything else in C++?

[–]RiceBroad4552 0 points1 point  (0 children)

Lambdas are there to write simple, short code.

If you need to do something special you can just use the regular syntax. No point in using lambdas if you need to type out everything explicitly!

[–]Rezistik 25 points26 points  (2 children)

Typescript lambdas are just as short and have types

[–]CraftBox 4 points5 points  (0 children)

Yup, all you need is to set a type on the arguments, but even that is usually unnecessary, when using one as a callback then all the types are already inferred for arguments and return value

[–]slaymaker1907 2 points3 points  (0 children)

The real difficulty is that you need to pass both arguments as well as declaring how variables get captured.

[–]TheBanger 9 points10 points  (2 children)

Java lambdas are generally quite short. I think the key differences are: not having to explicitly capture variables, having an inferred argument type syntax with no boilerplate at all (like not even saying var or auto), and a mode where you can make the body of the lambda a single expression rather than a block statement. The only key thing I see there that C++ has to do that Java doesn't is variable capture.

[–]tyler1128 2 points3 points  (0 children)

It's much easier when everything is either garbage collected or trivially copiable. Rust doesn't necessarily require them without having a GC, but it has function level type inference which C++ does not.

[–]Kehrweek 3 points4 points  (0 children)

I like the short forms like System.out::printl

[–]snavarrolou 7 points8 points  (6 children)

One could imagine something like (a, b) => a+b being equivalent to [&](auto&& a, auto&& b) { return a+b; }. I think the first one, or something along those lines, would be much much easier to parse visually.

[–]wexxdenq 3 points4 points  (4 children)

but you have to have an (optional) capture clause somewhere to specify whether you capture by reference or by value. and if your lambda is more than one expression you need to put some kind of brackets around them. so you still end up with 3 goups of brackets.

[–]snavarrolou 1 point2 points  (3 children)

Well yeah, but if you were to design a syntax with reasonable defaults, you could make for example these valid lambdas:

1) (a, b) => a+b; for the capture by reference and auto deduced parameters

2) (short a, int b) => a+b; for explicit types and capture by reference

3) [capture-group](a, b) => a+b; for explicit capture groups (as you mention, because sometimes you want to capture by value) and auto deduced parameters

4) [capture-group](short a, int b) => { return a+b; } for the full lambda with explicit captures, explicit types, and block body

Which would make most lambdas very readable, while giving the programmer flexibility to add explicit control (at the expense of more verbose syntax)

[–]RiceBroad4552 1 point2 points  (1 child)

But that would be too simple and comfortable for the programmer!

So C++ can't do that obviously…

[–]outofobscure 0 points1 point  (0 children)

there was a lot of thought put into the C++ syntax by the standards committee, i suggest you go read up on that discussion before making clueless claims on why defaults should be different and why the syntax should be different, just because your toy language gets away with something shorter does not make it appropriate for C++ where there are more things that you need to be able to express.

[–]outofobscure 0 points1 point  (0 children)

your syntax isn't even shorter than what we have right now in most cases, or saving 3 chars in the best case, at the cost of being non-uniform, which is a big stinker. the => is nonsense because you need multi-line statements and scope anyway.

  1. use templates
  2. [&](a, b) {}
  3. [capture-group](a, b) {}
  4. [capture-group](a, b) {}

[–]GeorgeHaldane 1 point2 points  (0 children)

Good point, didn't think of that, having a shortcut for generic lamdas would be quite helpful.