Live data from Hacker News

C++14 lambda tutorial

solarianprogrammer.com

11–20 of 32 posts

Re: C++14 lambda tutorial

#11
post #9

In python, lambda usage extremly beautiful. Also, 'lambda' keyword solves readibility problem. I expected a keyword. Is there any challenge to add keyword?

I don't like that in python you are enforced to only use a single expression in lambda. Sure, writing long lambda functions is ugly but I don't think python should enforce a given coding style in the language level.

Isn't that literally a core principal of Python? They enforce all kind of coding styles within the language, not the mention all the PEPs.

Re: C++14 lambda tutorial

#12
post #9

In python, lambda usage extremly beautiful. Also, 'lambda' keyword solves readibility problem. I expected a keyword. Is there any challenge to add keyword?

I don't like that in python you are enforced to only use a single expression in lambda. Sure, writing long lambda functions is ugly but I don't think python should enforce a given coding style in the language level.

That also dramatically reduces the value of lambdas. Without that limitation then they work as anonymous blocks of any complexity.

Re: C++14 lambda tutorial

#13
post #4

In python, lambda usage extremly beautiful. Also, 'lambda' keyword solves readibility problem. I expected a keyword. Is there any challenge to add keyword?

Not shown in this article are all the things you can do within the [] (typically just [&] or [=]), which turns a simple lambda into a closure. The syntax is a bit arcane, but it does what it needs to. They could've stuck a "lambda" keyword in front of it all, but it's not necessary, and making a new keyword always has the potential to break old code.

is it possible to do

#define lambda []

?

Re: C++14 lambda tutorial

#15
post #4

Earlier quoted context omitted.

Not shown in this article are all the things you can do within the [] (typically just [&] or [=]), which turns a simple lambda into a closure. The syntax is a bit arcane, but it does what it needs to. They could've stuck a "lambda" keyword in front of it all, but it's not necessary, and making a new keyword always has the potential to break old code.

is it possible to do #define lambda [] ?

I think that's possible, but I would strangle anyone who actually did that. At that point, not only are you pointlessly redefining the language just so it can feel slightly more familiar to people who are more comfortable with Python and the like, but you also completely lose the ability to enclose variables in the square brackets to make a closure.

If you're going to write C++, then write C++. Don't redefine language constructs to fit the patterns of your other favourite language.

Re: C++14 lambda tutorial

#16
I've never been a good at Templates. After reading this it seems that a lot of Template uses can be replaced with the generic lambdas using auto as the types going in.

Am I wrong or misunderstanding? I'm not saying completely but I feel like when I have used Templates I can now use this.

Re: C++14 lambda tutorial

#17

I've never been a good at Templates. After reading this it seems that a lot of Template uses can be replaced with the generic lambdas using auto as the types going in. Am I wrong or misunderstanding? I'm not saying completely but I feel like when I have used Templates I can now use this.

My understanding is that auto can serve some of the role of templates, but that has no particular tie to lambdas that I can see.

Re: C++14 lambda tutorial

#19
post #9

Earlier quoted context omitted.

I don't like that in python you are enforced to only use a single expression in lambda. Sure, writing long lambda functions is ugly but I don't think python should enforce a given coding style in the language level.

Isn't that literally a core principal of Python? They enforce all kind of coding styles within the language, not the mention all the PEPs.

But PEPs are not enforced in the language level. Also there are cases where there could fit two simple expression in a lambda. Also the language still doesn't stop you from abusing lambdas, you can nest them. It could be stated in one of the PEPs that "no overly complicated lambdas" and that's it, there is no need for the language to enforce this.

Re: C++14 lambda tutorial

#20

I've never been a good at Templates. After reading this it seems that a lot of Template uses can be replaced with the generic lambdas using auto as the types going in. Am I wrong or misunderstanding? I'm not saying completely but I feel like when I have used Templates I can now use this.

Templates are the C++ implementation of "generics". Their utility extends far beyond lambdas-as-callables. The entire STL library is built using the Template feature of C++. Templates are far more important than the ability to do "true" object-oriented programming using other features, in my opinion.

For a more concrete example that doesn't rely on the "authority" of an externally developed template library, consider that templates are also useful for static/compile-time polymorphism (see: Curiously Recurring Template Pattern).

Post reply on HN