On Thursday, 1 September 2016 at 12:04:51 UTC, qznc wrote:
On Thursday, 1 September 2016 at 08:04:00 UTC, Bienlein wrote:
D has a lot to offer with regard to functional programming. It
has pure functions and true immutable classes (true = also sub
objects become immutable), which Scala all doesn't have
(because of restrictions of the JVM). Does D have tail call
recursion optimization? I don't know, actually. If D had that
and pattern matching, it would beat Scala with all it's hype
by a big leap.
D does not guarantee tail calls, but the compiler might
sometimes decide to do it. Functional programmer usually want
to guarantee, so they don't have to write a loop. The downside
of TCO is that the stack trace is missing frames, which can be
very confusing.
There's a DIP about adding a TCO attribute/pragma:
https://github.com/dlang/DIPs/pull/6
FYI: There's some discussion about it's benefit in comparison to
functional language D does have loops and it's a lot easier to
write and read them.
If you want to chime in, please do so at the PR discussion.
D has no syntax for pattern matching, but some library support:
https://p0nce.github.io/d-idioms/#Recursive-Sum-Type-with-matching
Yep it's pretty easy to do within library code, if you use an
object chain you might also want to have a look at castSwitch.
https://dlang.org/phobos/std_algorithm_comparison.html#.castSwitch