News for hackers. Stuff that matters.
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.