News for hackers. Stuff that matters.
Re:Why I Use Elm in 2023 by bedobi on Tuesday May 23, 2023 @10:36AM (#36044934)
I think there's a case to be made for simpler languages, but not the way the author portrays it here, and not the way eg Go does it.

IMO the biggest problems with almost all popular programming languages are

1) null

2) exception based error handling

such that, when you call foo() where foo is

String foo(){ blabla }

you can get a String, null OR an exception (!!!) and most compilers happily let you treat it as if it only ever returns a String.

I hope some day null is no longer a thing, and that Functional Programming types like Option, Either, Try etc in the native libraries is the new default.

incredibly, there's still no consensus as to which exceptions should be used for what and when, though these days the most common approach is to simply stick only to RuntimeExceptions, which is terrible.

Functional error handling using Option, Either, Try etc are arguably much simpler, safer and more powerful than exceptions.

Simpler because they don't rely on dedicated syntax- they're just regular objects no different to any other object.

Safer because unlike exceptions, they force callers to handle all potential outcomes, but no more. (no risk of ignoring errors and no risk of catching a higher level of error than desired, ubiquitous bugs in exception based error handling)

Powerful because they support map, flatmap, applicative etc, making it easy to eg chain multiple computations together in desired ways, which is unwieldy and bug prone when using exceptions.

It could be that, when learning Java, Python and any other language, we learn that methods return objects... and that's that. No weird dedicated syntax and magic, special treatment for returning anything other than the happy path, and the HUGE complexity that comes with it, eg the dedicated syntax itself and how it behaves, differences between checked and unchecked exceptions, hierarchies of exceptions etc etc.

In addition to that, when lists and booleans implement map, flatMap etc, you can actually reduce syntax of languages even further- there's no need for looping syntax like for, while etc, and no need for if else either. This is probably too extreme for most people, but think about it.

Re:Why I Use Elm in 2023 by Buttons840 on Tuesday May 23, 2023 @10:45AM (#36045063)
I like exceptions. You can't get away from them, I know Haskell tried, Rust tried, they still have exceptions (perhaps they call them by a different name though). May as well make that system generally available to programers. They also allow you to have the Erlang/Elixir philosophy of happy path coding, "just let it fail", by using a high level exception handler and then just coding the happy path underneath.
Re:Why I Use Elm in 2023 by byby on Tuesday May 23, 2023 @11:00AM (#36045288)
>I like exceptions. You can't get away from them, I know Haskell tried, Rust tried, they still have exceptions (perhaps they call them by a different name though)

Rust has no exceptions in any sense of the word. Exceptions are ways to crash the program in a controlled manner with the ability for these crashes to be caught midway before the program crashes and handled correctly.

In rust you can only crash it explicitly using the panic! Macro. No handling.

The different name you are looking for is "errors". Rust has errors as do all programs. Some programs have exceptions to handle those errors. Rust does not.

If you mean "runtime crashes" then elm is the only language that is popular that uses has no runtime crashes.

Re:Why I Use Elm in 2023 by Buttons840 on Tuesday May 23, 2023 @11:37AM (#36045866)
You absolutely can catch and handle a panic in Rust [0]. Panics are just exceptions by a different name. Although, I do agree that the Rust ecosystem tries hard to avoid using panics, which is good.

[0] Example of catching, handling, and "swallowing" a panic in Rust (GTP4 wrote this): https://play.rust-lang.org/?version=stable&mode=debug&editio...

Re:Why I Use Elm in 2023 by awestroke on Tuesday May 23, 2023 @11:43AM (#36045967)
This is very uncommon to do and cannot be compared to exceptions
Re:Why I Use Elm in 2023 by byby on Tuesday May 23, 2023 @01:14PM (#36047457)
Learned something new. I feel though it's better if most programmers didn't know about this. It looks like cursed knowledge.
Re:Why I Use Elm in 2023 by dmytrish on Thursday May 25, 2023 @06:13AM (#36068880)
It's not exactly cursed knowledge, you just have to have enough judgement to understand where it's appropriate.

Trying to do magic and patch something on a panic and retry is cursed indeed. If it's about cleaning up resources of a thread and logging this somewhere, then there's nothing cursed about this.

Re:Why I Use Elm in 2023 by sriram_malhar on Tuesday May 23, 2023 @01:26PM (#36047652)
Catching a panic is _morally_ equivalent to catching a SIGSEGV. It is not meant for normal program error conditions and expected failures (e.g. "No such file").
Re:Why I Use Elm in 2023 by riwsky on Wednesday May 24, 2023 @02:15AM (#36054699)
I.e. only to be used in exceptional cases.
Re:Why I Use Elm in 2023 by Georgelemental on Tuesday May 23, 2023 @11:33AM (#36045799)
> Exceptions are ways to crash the program in a controlled manner with the ability for these crashes to be caught midway before the program crashes and handled correctly.

> In rust you can only crash it explicitly using the panic! Macro. No handling.

You can catch Rust panics with `catch_unwind()` if you compiled your program with `-Cpanic=unwind` (default on most platforms). Under the hood it uses stack unwinding, just like C++ and co, exceptions. But Rust doesn't consider this idiomatic error handling, it's only used in a few special situations (biggest ones are test frameworks, and web servers where one buggy session shouldn't take down the process).

Re:Why I Use Elm in 2023 by steveklabnik on Tuesday May 23, 2023 @11:34AM (#36045811)
I don’t disagree with you that just because rust has a feature similar to exceptions, that it has exceptions, but https://doc.rust-lang.org/std/panic/fn.catch_unwind.html does exist.
Re:Why I Use Elm in 2023 by bedobi on Tuesday May 23, 2023 @11:45AM (#36046009)
The problem is that virtually all languages abuse exceptions and use them for very much expected control flow and business logic. But exceptions should be just that, exceptional. Eg when you make a db call, the db not responding is not exceptional. Nor is not finding the user you're looking for. Those are 100% within the range of expectable outcomes for a db call.

fun findUser(userId: String) : Either<Error, User>

is MUCH simpler and more powerful than

fun findUser(userId: String) //haha, I can actually throw exceptions that aren't in this signature, or return null (wat)

Re:Why I Use Elm in 2023 by kwhitefoot on Tuesday May 23, 2023 @12:31PM (#36046665)
> I like exceptions. You can't get away from them

I hope you never program any safety critical machinery. I don't mean exceptions cannot be useful, just that there are circumstances where they are useful and circumstances where they are not.