News for hackers. Stuff that matters.
Re:Functional programming in C++ (2012) by jimbob45 on Monday December 28, 2020 @05:12PM (#25563867)
“The lack of any language support in C++ for maintaining purity is not ideal.”

I get that C++ is already bloated and bolted on to C as is but I would have preferred to see changes like these instead of sugary bloat like “auto”. Adding a “pure” keyword allows testability not otherwise attainable without modifying the compiler. Adding “auto” is just a me-too to the “var” of C# and Java.

Basically, I’m trying to say that C++ needs to have its standards committee fired.

Re:Functional programming in C++ (2012) by OskarS on Monday December 28, 2020 @06:15PM (#25564462)
> Adding “auto” is just a me-too to the “var” of C# and Java.

I get the "C++ is so full of nonsense, and they keep adding more of it!" attitude, and I largely agree with it, but "auto" is a bad target. auto makes a lot of c++ code (most, arguably) much easier to read and cleans up and simplifies a lot of the language. Like, the fact that you never, ever, have to write out the full name of iterator types in C++ (you can just go "auto it = vec.begin()" or whatever) is worth the feature all on its own. It also makes dealing with complex generics simpler. It's a huge quality of life improvement.

There's a reason most languages have added type inference, and it's not just a trendy bandwagon. In most reasonably recent languages (Rust and Swift notably), it is in fact the default to do this kind of type inference. It's genuinely a nicer way to program.

In C++ it also serves another important purpose: if there wasn't auto, there wouldn't be lambdas. All lambdas in C++ have an type created by the compiler which you can't access the name of. In order to store a lambda in its native type, you therefore have to use auto. Basically, if you like that C++ is becoming "more functional", that is only really possible because of adding auto.

Re:Functional programming in C++ (2012) by CodeGlitch on Monday December 28, 2020 @06:28PM (#25564587)
Agreed. Another way to think about it is the language is trying to get the compiler to do most of the heavy lifting. Stuff like auto makes sense with this attitude because why have the programmer think about what the type should be when it can be infered by the compiler? Also see templating.

I've come back to C++ after being away from it for a few years, and modern C++ (17, 20) is a delight to work with compared to what we had in the 90s. RAII especially...

Re:Functional programming in C++ (2012) by pjmlp on Tuesday December 29, 2020 @05:08AM (#25568399)
RAII was already available in C++ARM.

I was happily taking advantage of it in Turbo C++ 1.0 for MS-DOS.

Re:Functional programming in C++ (2012) by jimbob45 on Monday December 28, 2020 @06:42PM (#25564712)
I thought about lambdas before I wrote my answer but I thought decltype was and is more conventionally used for that purpose?
Re:Functional programming in C++ (2012) by MauranKilom on Monday December 28, 2020 @07:21PM (#25565042)
Can you elaborate what you envision there? For reference, a lambda in C++ might look like this:

    auto sqr = [](double val) { return val * val; };
(The lambda expression is technically only the part after the =, but storing it into a variable is common enough).

How would you use decltype here?

Re:Functional programming in C++ (2012) by kllrnohj on Monday December 28, 2020 @09:47PM (#25566185)
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p007...

It's been proposed already. One of the issues with the proposal that jumps out to me is that it wants memory allocation to be not allowed in a pure function. Which is both fair & not. That means no collections, more or less, as you can't allocate the return value.

But if you allow memory allocations, then you go down the rabbit hole of well whose memory allocations are allowed? Only `new`? What about `malloc`? Or a custom allocator? If custom allocators are allowed, do they need a `mutable` keyword equivalent but for pure functions, where it's internally not-pure but is allowed to be used from a pure function?

But it looks like there was no follow-up proposal. `constexpr` isn't too far off from a [[pure]] specifier, though.

Re:Functional programming in C++ (2012) by thechao on Monday December 28, 2020 @05:23PM (#25563966)
The stds committee is similar to a regulatory capture situation: all the people who really cared gave the fuck up.
Re:Functional programming in C++ (2012) by flatline on Monday December 28, 2020 @05:46PM (#25564178)
I can say this is not true for the general case. C++ has - against the odds, in my opinion - been dragged kicking and screaming into the 21st century. It is not exactly design by committee, because there are a lot of (semi)independent developers driving the design choices. The choice of features for a given release is by committee, however, and reflects a wide variety of opinions and interests. Not the least of which are the people actually providing compiler support. Functional programming in C++ is still not always facilitated by the standard library, but it's miles beyond where it was a decade ago: moving in the right direction.
Re:Functional programming in C++ (2012) by iatt on Monday December 28, 2020 @06:14PM (#25564460)
I agree on the merits of "pure", but I wouldn't call "auto" bloat. As a quality-of-life improvement, especially for writing generic code, it's pretty useful.
Re:Functional programming in C++ (2012) by petters on Monday December 28, 2020 @06:12PM (#25564442)
In C++20, e.g. std::vector is constexpr, so this keyword may be similar to your hypothetical "pure" soon.
Re:Functional programming in C++ (2012) by petters on Friday January 01, 2021 @07:02AM (#25602677)
Or maybe "consteval," which I was not aware of: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p107...
Re:Functional programming in C++ (2012) by voldacar on Monday December 28, 2020 @05:16PM (#25563892)
D has a "pure" keyword and it works quite nicely. Agreed about the standards committee lol
Re:Functional programming in C++ (2012) by moonchild on Monday December 28, 2020 @05:22PM (#25563953)
D's pure doesn't work very well at all.

Because there's no ecosystem of pure functions, a single impure function in the transitive closure of your dependencies can ruin your day. (Unless you depend on no code written by other people.)

Compare with haskell, where effects are enumerated and purity is the default: most functions are pure.

Re:Functional programming in C++ (2012) by voldacar on Monday December 28, 2020 @08:07PM (#25565409)
> Unless you depend on no code written by other people.

Yeah that is me. I can certainly see how a program that uses a lot of libraries would have issues with pure though. Not sure what the solution to that would be other than going full haskell mode, which isn't what people who use C++/D would want anyways

Re:Functional programming in C++ (2012) by mhh__ on Monday December 28, 2020 @06:59PM (#25564884)
Idiomatic D code tends to use a lot of templates so this is true but much more prevalent if you write C-with-pure than (say) the code I tend to write (an incomplete C preprocessor+binding generator at compile time is the most D-ish thing I've done yet)