News for hackers. Stuff that matters.
Re:Ask HN: Will Rust ever become a mainstream systems progra... by pcwalton on Monday April 10, 2017 @05:06PM (#14082554)
We would lose lots of users from C and C++ if we mandated exceptions for error handling. The code size and complexity of unwinding is too much of a price for many to pay.
Re:Ask HN: Will Rust ever become a mainstream systems progra... by Animats on Tuesday April 11, 2017 @12:21AM (#14084826)
Rust is already paying the cost of unwinding. RFC 1236 added unwinding machinery for catching panics.[1] ("A drawback of this RFC is that it can water down Rust's error handling story.") This is the expensive part of exception handling. Rust has exceptions, but they're not very good.

Go also backed into adding most of the machinery for exceptions by adding catch with unwind for panics. If you're going to have the unwinding machinery, why not have real exceptions?

Exceptions aren't so bad if you do them the way Python does, using a predefined exception hierarchy. Catching an exception catches everything in its subtree. Catching RuntimeError catches most of the things one can induce from outside the program. Python's WITH statement also has one of the few mechanisms which can gracefully handle an error on releasing a resource. Exceptions in C++, Java, and Javascript are more troublesome.

[1] https://github.com/rust-lang/rfcs/blob/master/text/1236-stab...

Re:Ask HN: Will Rust ever become a mainstream systems progra... by dbaupp on Tuesday April 11, 2017 @12:38AM (#14084896)
You have talked about Python's exception scheme before, and been rebuffed before (it is too dynamic to use pervasively in a systems language), please be more specific, and also have the minimum grace to not repeat incorrect things.

In any case, panics are not guaranteed to unwind or be catchable: they can be turned into aborts (yes, on the stable compiler). This means an arbitrary library using them for recoverable error handling is incorrect.

Re:Ask HN: Will Rust ever become a mainstream systems progra... by Animats on Tuesday April 11, 2017 @01:02AM (#14085001)
What dynamism? An exception hierarchy just requires a type hierarchy, and Rust has enough inheritance for that. Rust has some dynamic dispatch, although it's not clear if the mechanism for trait objects [1] can implement an exception hierarchy cleanly.

A WITH clause mechanism implies no dynamism.

Please be more specific with your complaints. Thanks.

[1] https://doc.rust-lang.org/book/trait-objects.html

Re:Ask HN: Will Rust ever become a mainstream systems progra... by dbaupp on Tuesday April 11, 2017 @01:30AM (#14085094)
Firstly, "just" determining membership of a value within a type hierarchy requires far more dynamism than zero. Given a fairly arbitrary type-erased value x, how do you tell if its type implements some fairly arbitrary trait T? This hierarchy presumably has to be open, which makes the problem harder.

Additionally, unless one is using explicitly typed exceptions (i.e. Java-style checked exceptions, very much unlike Python), it requires allocations, as it's not possible to statically determine the size of the error object to be returned.

Similarly, from an assurance/reliability point of view, the dynamically typed nature of non-checked exceptions is unfortunate.

(These second two are, I believe, the points I made last time we discussed Python's exceptions.)

Re:Ask HN: Will Rust ever become a mainstream systems progra... by Animats on Tuesday April 11, 2017 @01:56AM (#14085201)
Having to allocate memory while processing an exception probably isn't a big deal, since Rust does not claim to be able to operate in an out-of-memory condition. Besides, unwinding already causes allocation:

"We'll also need a way to handle out-of-memory (OOM) conditions. The standard library calls the abort intrinsic, which just calls an illegal instruction to crash the whole program. The reason we abort and don't panic is because unwinding can cause allocations to happen, and that seems like a bad thing to do when your allocator just came back with "hey I don't have any more memory"."[1]

[1] https://doc.rust-lang.org/nomicon/vec-alloc.html

Re:Ask HN: Will Rust ever become a mainstream systems progra... by dbaupp on Tuesday April 11, 2017 @02:13AM (#14085278)
> Having to allocate memory while processing an exception probably isn't a big deal, since Rust does not claim to be able to operate in an out-of-memory condition

Allocating is definitely a big deal for a pervasive error handling! For instance, parsing a number might not succeed. Both the actual parsing and the failure cases are cheap, but not if the latter has to be indicated by allocating and unwinding and doing a pile of the dynamic checks up the stack.

Additionally, whether the language is able to operate in OOM isn't the only reason to choose an error handling scheme. However, even if it was, I don't think it applies directly to Rust: Rust-the-language (and Rust-the-core-library) can operate in an OOM condition, e.g. people run Rust code without needing a dynamic allocator at all. You are correct that some parts of the standard library don't handle OOM, but, using exceptions pervasively means that there is one error handling scheme for things that can rely on having an OS/malloc and one for things that don't need them.

> Besides, unwinding already causes allocation

Yes, and that is one reason why unwinding is not used as a pervasive error handling scheme in Rust: there are various fairly fundamental problems with it. It is one of the reasons why panics can be switched to abort.

Re:Ask HN: Will Rust ever become a mainstream systems progra... by soulbadguy on Tuesday April 11, 2017 @12:32PM (#14089712)
> Allocating is definitely a big deal for a pervasive error handling!

Exceptions are not meant for "pervasive error", they are for exceptional execution paths. If an execution path is frequent enough, wether it's an "error" or not, then it should not be an exception.

I think this is the part of the argument that the "no exception camp" usually misses : there are multi type of errors (recoverable vs fatal, frequent vs unfrequent, expected vs unexpected), having exception is not about using exceptions for all of them

Re:Ask HN: Will Rust ever become a mainstream systems progra... by dbaupp on Tuesday April 11, 2017 @03:34PM (#14091626)
As the sibling points out, this is in the context of the suggestion of using Python's approach, where exceptions are not just pervasive, they are so pervasive they're used for non-errors/control flow (e.g. StopIteration).
Re:Ask HN: Will Rust ever become a mainstream systems progra... by steveklabnik on Tuesday April 11, 2017 @12:57PM (#14089973)
To be fair to the "no exception" camp, they do see that there are multiple kinds of errors: they partition them into recoverable errors and unrecoverable errors.
Re:Ask HN: Will Rust ever become a mainstream systems progra... by poopsicle on Tuesday April 11, 2017 @01:32PM (#14090382)
And yet the bulk of this thread was in reference to python where exceptions are the pervasive scheme.
Re:Ask HN: Will Rust ever become a mainstream systems progra... by fjrieiekd on Tuesday April 18, 2017 @08:43AM (#14138332)
There's also Swift's approach, which looks like exceptions but is really similar to Rust's approach under the hood. Swift also requires failable calls to be prefixed by "try" which makes them even easier to spot than the postfix "?" that Rust uses.
Re:Ask HN: Will Rust ever become a mainstream systems progra... by soulbadguy on Monday April 10, 2017 @05:31PM (#14082743)
Not mandate them , use have them as an options where the monadic Errors types are not enough or are too cumbersome to use.
Re:Ask HN: Will Rust ever become a mainstream systems progra... by pcwalton on Monday April 10, 2017 @05:34PM (#14082762)
And thereby prevent people from writing code that needs to catch errors but can't use exceptions. Won't work.
Re:Ask HN: Will Rust ever become a mainstream systems progra... by soulbadguy on Monday April 10, 2017 @06:00PM (#14082927)
I don't understand what that sentence means...
Re:Ask HN: Will Rust ever become a mainstream systems progra... by thristian on Monday April 10, 2017 @07:00PM (#14083325)
Some people don't want to use exceptions because of runtime overhead costs.

If exceptions are just "an option" that a library can choose when designing its interface, then you'll get a split ecosystem: a bunch of libraries that do use exceptions for error-handling, and then a separate bunch of libraries that do the same thing, but don't use exceptions for error handling, which is wasted, redundant effort.

Re:Ask HN: Will Rust ever become a mainstream systems progra... by btschaegg on Tuesday April 11, 2017 @02:03AM (#14085231)
> then you'll get a split ecosystem

Agreed. Point in case: Have a look at D and the discussions about their GC.

I haven't kept up much about the last year, but my general impression runs down to: If you want to use their standard library. There's gonna be garbage collection. Period. If you want to avoid it, enjoy implementing its functionality all by yourself.

With rust and exceptions you would likely not have a problem with the main ecosystem, but the same problem is bound to pop up with some big & famous library.

Edit: Similarly, exceptions are one of the reasons C++'s SG14 exists.