Required Reading: "Functional Programming in C++"

Jonathan M Davis newsgroup.d at jmdavisprog.com
Sat Oct 28 23:26:11 UTC 2017


On Saturday, October 28, 2017 21:51:50 Ola Fosheim Grøstad via Digitalmars-d 
wrote:
> On Wednesday, 25 October 2017 at 22:19:21 UTC, Walter Bright
>
> wrote:
> > for core D devs. Of course, this is much easier in D than in
> > C++ because of D's const and pure attributes.
>
> Nah, the type system isn't critical for doing functional style
> programming. People who want to do functional programming in C++
> has plenty of opportunities, and libraries to support it…

The type system can help ensure the correctness of functional programming
(e.g. pure helps ensure that you didn't accidentally do something involving
global variables), but it's certainly not required to write in that style.

And actually, most D code that's functional doesn't do much with either
const or pure. Most of the functional code is range-based, which
fundamentally falls apart with const - at most, const or immutable is used
for the elements. And while pure is often involved, it's typically only by
inference. So, in those cases, the only places that pure actually gets
checked is in unittest blocks marked with pure or when you try to use a set
of range-based operations inside of a function that you've marked as pure.
And range-based operations that definitely aren't pure are done on a regular
basis (e.g. any lazy ranges grabbing their elements from I/O).

So, arguably, D is already showing how unnecessary const and pure are for
functional programming. They can help, to be sure, but they aren't
necessary.

- Jonathan M Davis




More information about the Digitalmars-d mailing list