News for hackers. Stuff that matters.
Re:The general value of typed functional programming lies in... by nicoburns on Monday February 24, 2020 @10:37AM (#22404882)
Forget the performance or the fancy memory management stuff, this is one of the best things about Rust: it makes you deal with edge cases by default (while allowing you to write code in a largely imperative or functional-lite style).

It's why I think it competes for market share with some of what is currently Java/C# and similar languages. A lot of people who use those languages care about correctness, and Rust is big step up in this regard.

Re:The general value of typed functional programming lies in... by jerf on Monday February 24, 2020 @11:23AM (#22405415)
This is also one of the problems with programming with exceptions. You lean on the fact that an exception will be thrown and just propogate up until it finds something, so the normal mindset of an exception-based programmer is to program for the happy-case and only vaguely think about what happens if it goes wrong. I know, I've written a lot of code in this style for many years myself.

In some cases, that's not a problem or it's even the best thing to do. However, you end up writing this way all the time in an exception-based language, which is where the problem arises.

Non-exception based languages tend to throw the problems in your face and make them something you can't ignore. Which is not fun. But it is often what you need.

Re:The general value of typed functional programming lies in... by xamuel on Monday February 24, 2020 @12:13PM (#22405962)
This is especially evident in e.g. third-party Python libraries.

For example, say you have a pandas dataframe with a column "foo" and you accidentally try to read column "fooo". Instead of getting a useful exception like "PandasException: Attempted to read nonexistent column 'fooo'", you get a traceback 10 layers deep, terminating in something like "pandas.hashtable.PyObjectHashTable.get_item" which ought to be totally invisible to you, the library user.

Re:The general value of typed functional programming lies in... by fjp on Monday February 24, 2020 @01:29PM (#22406818)
This is how it should be done, I'm such a fan of this page: https://www.psycopg.org/docs/errors.html

Edit: by "this" I meant to agree with parent: Getting a useful, well defined exception is how it should be done. Psycopg2 does this very well.

Re:The general value of typed functional programming lies in... by arcticbull on Monday February 24, 2020 @12:34PM (#22406174)
Exceptions are the worst because there's a hidden nested world of control flow you can't barely begin to reason about. Exceptions are a "comes-from" statement, like a "goto" but much worse.
Re:The general value of typed functional programming lies in... by fjp on Monday February 24, 2020 @01:30PM (#22406839)
This just gave me the thought... if exception stack traces came with the argument values of each method call, they would be 1000x more helpful. You wouldn't even have to attach a debugger and step through in many cases.
Re:The general value of typed functional programming lies in... by Izkata on Monday February 24, 2020 @01:37PM (#22406920)
I think they do in python, or at least can be retrieved. I know they show up in the stack trace on Sentry when an unhandled exception occurs.
Re:The general value of typed functional programming lies in... by fjp on Monday February 24, 2020 @01:44PM (#22407002)
Interesting, I guess they don't get spit out to the console by default.

Overall Python has really good stack traces, but the hyper-dynamic nature of so many common libraries does make it tough to grok sometimes.

I usually end up reaching for the PyCharm debugger which is fantastic anyway.

Re:The general value of typed functional programming lies in... by fulafel on Monday February 24, 2020 @01:52PM (#22407092)
Re:The general value of typed functional programming lies in... by latch on Monday February 24, 2020 @12:12PM (#22405953)
There's also the OTP model, where you have exceptions as well as a robust "something" up there. You don't have to deal with the minutiae, and the pattern (processes (aka, isolated processes) and restarts) lets you deal with known or unknown errors.
Re:The general value of typed functional programming lies in... by hopia on Monday February 24, 2020 @02:16PM (#22407341)
Right, but the OTP doesn't make you deal with the exceptions upfront, only to architect your app in the way that they don't crash or infect other parts of the system.
Re:The general value of typed functional programming lies in... by lonelappde on Monday February 24, 2020 @05:06PM (#22408953)
That doesn't help you detect logic bugs, only bad input.
Re:The general value of typed functional programming lies in... by LessDmesg on Monday February 24, 2020 @01:38PM (#22406940)
Exceptions and Maybe types aren't mutually exclusive. They should be used together. Maybe types handle variability that is expected and predictable, like a dictionary not having a certain key; exceptions handle the unexpected situations, like a file handle being closed too early.

Maybes explicitly express what the programmer has foreseen, exceptions form a safety net over it. You need both in a wholesome language. That's wht even Haskell has exceptions.

Re:The general value of typed functional programming lies in... by nicoburns on Monday February 24, 2020 @05:07PM (#22408966)
Having used Rust, which almost exclusively uses Maybe types, I have to disagree. The "safety net" introduced by exceptions just hides possible errors, and causes reliability problems that would be entirely avoidable if Maybe types were used. The `?` operator makes propogating errors painless if that is what you want, but at least they're all documented so you have to make a concious chocie to pass the buck on error handling.
Re:The general value of typed functional programming lies in... by renox on Monday February 24, 2020 @05:58PM (#22409347)
I don't know: if we really wanted to be 'strict' every integer operation should return a Maybe due to possible overflow, every IO operation also.

That's a lot of possible error to handle! I don't like exception's invisible flow but I think that they are good for 'should never happen' errors.

Re:The general value of typed functional programming lies in... by cptskippy on Monday February 24, 2020 @12:13PM (#22405968)
> the normal mindset of an exception-based programmer is to program for the happy-case and only vaguely think about what happens if it goes wrong.

I'm not sure that's such a bad thing. Exception handling doesn't mean you should be handling all exceptions, just that you can recover if one does occur. If an exception is 100% relevant to what you're doing then yes absolutely handle it, or even clean up your own state before propagating the exception if you can't. But please don't catch any and all exception to suppress or obfuscate them by wrapping it in another domain specific exception.

Re:The general value of typed functional programming lies in... by arcticbull on Monday February 24, 2020 @12:35PM (#22406190)
> Exception handling doesn't mean you should be handling all exceptions, just that you can recover if one does occur.

You can't because you don't know where it came from so you don't know what happened, why, or if it was recoverable. You can take a stab at it, but that's it.

Re:The general value of typed functional programming lies in... by cptskippy on Monday February 24, 2020 @01:55PM (#22407126)
> You can't because you don't know where it came from

Maybe that's a particular language constraint? In general exceptions have a stack trace and it's evident where an exception came from.

At least in my experience exceptions don't have to be handled unless or until they're disruptive. If you have a scheduler or task processor code that needs to run continuously then you're going to have robust exception handling, but not necessarily comprehensive or appropriate. For other areas of code however the exception handling isn't necessarily requisite.

Re:The general value of typed functional programming lies in... by nicoburns on Monday February 24, 2020 @05:11PM (#22408991)
My problem with exceptions, is it's generally not possible to tell which exceptions a given function might throw (especially problematic with nested dependencies). Thus even if there was a condition hat you could handle you might not know it was possible (until you hit the bug in production!)

Result/Maybe types are awesome, because you just don't get unexpected runtime exceptions due to programming error. You only get them when a genuine problem occurs.

Re:The general value of typed functional programming lies in... by cptskippy on Tuesday February 25, 2020 @10:03AM (#22413705)
> it's generally not possible to tell which exceptions a given function might throw

That's a valid concern. I've seen attempts to document the custom exception types that classes can throw but it's never discoverable. It's compounded by the fact that uncaught exceptions bubble up.

I don't know that most people would sift through all the possible exceptions and write selective catch blocks to handle them. I think in general they will either catch all exceptions or none, if or until they need to handle a specific scenario that presents itself in production.

My perspective though is primarily of LOB Apps or Integrations and not widely consumed public code or apps.

Re:The general value of typed functional programming lies in... by juskrey on Monday February 24, 2020 @12:16PM (#22406001)
Rust forces you to deal with edge cases using very low-order, often Procrustean bed reasoning. This does not mean there are much more powerful higher level ways of doing this. (Pattern matching I got in exchange have made me cry)
Re:The general value of typed functional programming lies in... by dpc_pw on Monday February 24, 2020 @11:28PM (#22410910)
That is simply not true.
Re:The general value of typed functional programming lies in... by juskrey on Tuesday February 25, 2020 @04:58AM (#22412022)
If it forces you to check every single return code, this does not mean you can't have equivalently safe implementation without that level of detail.
Re:The general value of typed functional programming lies in... by jayd16 on Monday February 24, 2020 @10:55AM (#22405119)
You can use a functional style in those languages as well. Or do you mean with Rust's borrow checker you have less mutability to worry about?
Re:The general value of typed functional programming lies in... by seren on Monday February 24, 2020 @11:08AM (#22405273)
If you have a Rust enum with Color{Red,Green,Blue), if for some reason you update to Color{Red,Green,Blue,Yellow}, the rustc compiler will force you to manage the new Yellow case wherever you want to "match" a pattern on Color, so overall you have less chance to forget to manage the new case everywhere. This kind of error is not managed by a C or C++ compiler (as far as I know).

But from my understanding, this is not directly related to the borrow checker, which tracks who owns a reference and is allowed to modify it or not.

(That being said if you had a "default" case earlier, the compiler won't complain)

Re:The general value of typed functional programming lies in... by ZephyrP on Monday February 24, 2020 @11:16AM (#22405352)
If you compile with `-Wall` GCC will emit a warning for failing to exhaustively check enum values inside a switch statement

(I don't remember the specific underlying feature flag name, sorry).

EDIT: It's called `-Wswitch`

Re:The general value of typed functional programming lies in... by seren on Monday February 24, 2020 @11:22AM (#22405406)
Yes, apparently, there is a -Wswitch statement in gcc, which looks quite similar, maybe I have used too old compilers...

> Warn whenever a switch statement has an index of enumerated type and lacks a case for one or more of the named codes of that enumeration. (The presence of a default label prevents this warning.) case labels outside the enumeration range also provoke warnings when this option is used (even if there is a default label). This warning is enabled by -Wall.

Re:The general value of typed functional programming lies in... by SAI_Peregrinus on Monday February 24, 2020 @12:37PM (#22406205)
The issue with -Wswitch (and thus -Wall) is that the presence of a default case suppresses the warning. Which you may not want, if you need non-default behavior because you're adding a new value! There's -Wswitch-enum (not turned on by -Wall or -Wextra) that stops this being suppressed. This is good, though the (possible) list of cases that are well-handled by the default at the end of switches that just fall-through can be ugly.
Re:The general value of typed functional programming lies in... by marcosdumay on Monday February 24, 2020 @04:43PM (#22408764)
A default case supreses the warning in Haskell too, there is no other option.

The real problems with C enums aren't about matching completion, they are that:

- There is no guarantee that a variable value is in the enum range;

- A C (or Java, or C#) enum is a very poor type and can not represent all the data that a Rust enum carries. Developers usually use them with union types to solve that problem, but then there aren't any warnings for most problems anymore.

Re:The general value of typed functional programming lies in... by nicoburns on Monday February 24, 2020 @05:56PM (#22409328)
The big difference is that C++ emums can't contain data. C++ has checking on enums, but enums are a relatively niche data structure for specific use cases.

In Rust, enums are a core data structure with completely equal status to structs, and are used pervasively throughout the language including the standard library.

Notably, both Option (which is used instead of null) and Result (which is used for error handling) are enums, which means that you thse correctness checks for every null and every error condition, on by default. Thats a huge deal as these are often the source of unexpected runtime errors.

Re:The general value of typed functional programming lies in... by jcelerier on Monday February 24, 2020 @11:27AM (#22405462)
> This kind of error is not managed by a C or C++ compiler (as far as I know).

C++ had boost.variant which did exhaustiveness checking since ... 2002 (and an std version since C++17).

Re:The general value of typed functional programming lies in... by hellofunk on Monday February 24, 2020 @01:02PM (#22406487)
Many people have their compilers set to enforce these things, and many of us go a step further and make our compiler warnings into errors so it’s impossible to build a program with these errors. That’s very commonplace, and C++ does it just fine. Missing cases in enums and at least 100 other very common matters can be checked at compile time in C++, the only difference is that it’s not the default as it is in some other languages.
Re:The general value of typed functional programming lies in... by LessDmesg on Monday February 24, 2020 @01:42PM (#22406988)
Enums are no substitution for sum types, though.
Re:The general value of typed functional programming lies in... by hellofunk on Monday February 24, 2020 @03:08PM (#22407894)
The comment I was responding to was not about the functionality of a sum type, but rather the compilation errors from leaving out a case. Those are two different matters.
Re:The general value of typed functional programming lies in... by The_rationalist on Monday February 24, 2020 @01:32PM (#22406860)
Kotlin also enforce exhaustivity in when(), for enums and ADTs (sealed class's)
Re:The general value of typed functional programming lies in... by AsusFan on Monday February 24, 2020 @11:23AM (#22405417)
This isn't particular of Rust. Nim also forces you to deal with all possible branches of a case statement. I'm pretty sure other languages do so as well.

This is just basic type safety stuff.

Re:The general value of typed functional programming lies in... by ZephyrP on Monday February 24, 2020 @11:32AM (#22405504)
plenty of other languages do so, but it's both unfairly diminishing to the parent & questionably correct to refer to it as "basic type safety stuff"
Re:The general value of typed functional programming lies in... by hopia on Monday February 24, 2020 @02:18PM (#22407372)
On the other hand, it's not a very advanced type level feature either to insist on total functions. I'm sure Rust's type system goes a lot further in enforcing a good level of type soundness.

Edit: typo

Re:The general value of typed functional programming lies in... by a1369209993 on Monday February 24, 2020 @03:51PM (#22408302)
No, it is basic. If you have a sum type, and you have no defined behaviour for one of the terms of the sum type, your code is not type-safe and a language that aspires to "basic type safety stuff" should at least issue some kind of error about it. In a putatively statically-typed language (ie one that does type checking at compile time), you'd expect that error to happen at compile time.
Re:The general value of typed functional programming lies in... by threatofrain on Monday February 24, 2020 @11:46AM (#22405651)
JS has this too in TS, so I’m pretty sure the perception among users of typed languages is that this is a basic part of what type systems do. What does this have to do with FP?
Re:The general value of typed functional programming lies in... by nicoburns on Monday February 24, 2020 @05:14PM (#22409015)
TS actually has quite an advanced type system compared to languages like Java.
Re:The general value of typed functional programming lies in... by woah on Monday February 24, 2020 @11:08AM (#22405274)
The borrow checker takes care of a lot of the things that many functional patterns are supposed to prevent, such as referencing values in two locations.