News for hackers. Stuff that matters.
Re:Ill-Advised C++ Rant, Part 2 by Animats on Monday March 07, 2016 @01:18AM (#11237416)
Rust encourages writing imperative code in a functional style, like this:

    fn run_query() -> Result<PgResultSetOrWhatever, String> {
        PostgresConnection::connect("postgres://localhost:5432/postgres", &NoSsl)
            .and_then(|conn| conn.prepare("SELECT ir FROM x"))
            .and_then(|stmt| stmt.query([]))
            .map_err(|e| format!("{}", e))
    }
This is a strange way to write control structures. Each object gets to define its own control structure syntax. Then there's the "try!" macro, which generates an invisible return on error. Cargo has their own "try!" macro, and it's slightly different. All this puts a layer of macros on top of control flow. There's a rationale for that, but it doesn't help readability.

Exceptions were such a mess in C++ that they've scared people away from the concept. But they work well in Python, especially in conjunction with "with" clauses. The machinery in Rust to avoid exceptions is more complex than exceptions. It took C++ years, and Boost, to get to this level of wallpapering over a mess.

Re:Ill-Advised C++ Rant, Part 2 by dbaupp on Monday March 07, 2016 @01:37AM (#11237466)
> Each object gets to define its own control structure syntax.

This is true in every language: types get to define their API, and languages with first-class functions (or something similar) get to define things that act like a control structure.

In any case, almost all interactions like that go through Option or Result, i.e. people are almost never defining them themselves, it's all standard. (Of course, it isn't quite as standard as, say, the Monad and do-notation of Haskell, which cover a sizeable portion of the space of possible reasons to implement ones own control flow, all with the same syntax.)

> Cargo has their own "try!" macro, and it's slightly different.

I don't think this is true, AFAICT cargo just uses the standard one. That said, I have this vague recollection that, a while ago, cargo used to use the current definition of `try!`, while the one in the standard library was strictly less flexible. That is, cargo was serving as a prototype of the generalised try! that's now standard.

> But they work well in Python, especially in conjunction with "with" clauses.

It's not immediately obvious what difference you see between Python exceptions and C++ ones. It seems to me that a lot of the niceness of Python exceptions (conversely, difficulty of C++ ones) are driven by other choices in language design, and Rust generally trends toward C++ for choices like this.

In any case, "with" clauses are... something Rust doesn't have a strong need for, or, more specifically, Rust (and C++) already handles 99% of the use-cases for them. "with" clauses are designed as a way to have scoped-based resource management in a language without timely clean-up, and so don't make nearly as much sense when the language already gives that.

Re:Ill-Advised C++ Rant, Part 2 by Manishearth on Monday March 07, 2016 @02:00AM (#11237534)
The try macro is going to be replaced by cleaner syntax soon (along with control flow based catch syntax). It's not "a layer of macros", its one macro, which everyone knows about, so it's not invisible. No different from a return or throw statement -- the control flow escape hatch is "invisible" there, too, but everyone knows what a return/throw are, so it's perfectly visible. It's the same situation with try -- everyone knows what it does; so it's not invisible.

Rust doesn't encourage writing things monadically. You can write them as nested if lets if you want; indeed; many people do exactly that (I prefer doing this too, or using try).

Where's the wallpapering? There's try!, and a couple of monadic methods on Result, and that's about it? Monadic error handling is not a new idea, and it's not really complicated either (well, if you force people to understand monads first, it is, but that's totally unnecessary and nobody does that). This is no more complicated than vanilla C++ exceptions. It's different, and different from what people are used to, but not new.

Also, this isn't even part of the language, it's part of the stdlib. If anything that is a point for Rust, since C++ needs language integration for exception handling, Rust doesn't (and thus the language is simpler in this axis). It will soon become a part of the language; but only as some sugar.

And this is a very specific example. Overall, where does "Rust start at C++s complexity level"?

> Each object gets to define its own control structure syntax.

Technically only Result and Option do, the objects above are just Results. While you can create your own enums for error handling, most people don't, so there's no repetition of control flow syntax.

This is true anywhere, each object always gets to define its own utility methods. You have the same on the std::exception types in C++.

> The machinery in Rust to avoid exceptions

This is not "to avoid exceptions", it shouldn't be viewed that way. Sure, the Rust designers don't want exceptions in the language, but monadic error handling is a proper, tried-and-tested solution for error handling, not a "last resort".

Re:Ill-Advised C++ Rant, Part 2 by cousin_it on Monday March 07, 2016 @11:30AM (#11239754)
For what it's worth, I agree with Animats that Rust's error handling is too complicated.

My dream language would have catchable, unchecked, untyped exceptions. I.e. you can throw and catch strings anywhere without declaring that upfront. You can even throw from a destructor while unwinding, whereupon the new string gets appended to the old string and life goes on. It's a pretty sweet design:

1) Easy to read code with standard control constructs.

2) No performance overhead in the common case.

3) No dispatching on error types, therefore less temptation to use errors for control flow.

4) No distinction between recoverable errors (option) and unrecoverable errors (panic). I feel that distinction is in the eye of the beholder, especially if you need to catch errors from code you don't control.

5) No distinction between code that can cause errors and code that cannot, thus higher-order functions become easier to write.

I'm hard pressed to name any advantages of Rust's model compared to the above. Rust can't even claim to be transparently callable from languages that don't support exceptions, because panics exist :-(

Re:Ill-Advised C++ Rant, Part 2 by Manishearth on Monday March 07, 2016 @01:57PM (#11240730)
> It's a pretty sweet design

Yeah, it is, but it wouldn't interact well with Rust's safety guarantees :)

(You're analysing this feature in a vacuum, but that's not how language design is done.)

> Easy to read code with standard control constructs.

I think this is in the eye of the beholder, `try!` or `?` or `catch` are (would be -- for the latter two) easy to read for Rust folks. It's just different. do-notation in Haskell is similarly "standard" for functional types, but it's totally alien to C++ folks. The main reason folks find try/catch/throw easier to understand is because they're used to it. Once I learned how enums worked in Rust, `Result` was a very straightforward and simple thing. I actually personally find `try!` easier to read, since I know exactly where a return can happen, unlike C++ where the control flow is totally obscured.

This leads to the rug getting pulled out from underneath you, which can be bad for safety. In fact, Rust's `recover()` needs to be careful about types allowed to cross a recover boundary so that it can be 100% memory safe.

So the code might be easy to read, but not easy to reason about.

> No dispatching on error types, therefore less temptation to use errors for control flow.

I don't get what you mean here. Rust doesn't dispatch on error types, though it does dispatch on Result (which may contain an error). I don't see what's wrong with this. This lets you program in a functional way (and like I mentioned, you don't have to), which isn't a bad thing.

Errors _are_ a control flow thing, you can't escape that. C++ handles the control flow around errors with try, throw, and catch, Rust does it with Result and its methods (and the try macro).

> especially if you need to catch errors from code you don't control.

`recover()` exists. Use it sparingly; it's basically only for cases when you want to catch panics in code you don't control (even then, try other solutions), or stop panics from crossing FFI boundaries.

Panics are supposed to be for irrecoverable or impossible things, where "irrecoverable" is usually a statement that only makes sense in an application, not a library. So this problem in theory shouldn't come up (I haven't seen it happen much in practice).

I think the problem of forgetting to catch an exception from code you don't control because you don't know it's there is a much more pressing problem than having stray panics (since panics are relatively rare).

> No distinction between code that can cause errors and code that cannot, thus higher-order functions become easier to write.

Result<T,E> is also a type. Treat it as any other return type in a higher order function and it works. In `Fn(...) -> T`, `T` can be a Result type, no problem.

> Rust can't even claim to be transparently callable from languages that don't support exceptions, because panics exist

and so does `recover()`.

> No performance overhead in the common case.

IIRC it doesn't turn out to be much, but ICBW. I think someone looked into this.

Re:Ill-Advised C++ Rant, Part 2 by cousin_it on Monday March 07, 2016 @02:50PM (#11241121)
About higher order functions: putting effects in the type forces you to make a distinction between map and mapM. You can't unify them because one takes a -> b and the other takes a -> m b. Same for any other higher order function, you have to write two versions (or more if you don't have HKT).

About memory safety: can you give an example where a naive implementation of catchable exceptions would break memory safety? I know about RecoverSafe but it seems to be solving a different problem (marking types whose logical invariants are preserved in case of panic).

Re:Ill-Advised C++ Rant, Part 2 by Manishearth on Monday March 07, 2016 @03:04PM (#11241204)
> forces you to make a distinction between map and mapM. You can't unify them because one takes a -> b and the other takes a -> m b.

You have the same distinction in exceptions? One map would bubble the exception, the other would internally catch and exclude. You don't have to write two versions, map still works for cases with and without Result, but it will not have the additional behavior of excluding errors. You want that additional behavior, hence you write mapM; just like you would in C++.

> About memory safety: can you give an example where a "naive" implementation of catchable exceptions would compromise memory safety? I know about RecoverSafe but it seems to be solving a different problem (marking types whose logical invariants are preserved in case of panic).

There's an example in the rfc: https://github.com/rust-lang/rfcs/blob/master/text/1236-stab...

In Rust logical invariants are used to enforce memory safety. If the destructor of something within a vector panics, that might result in an invalid vector. There's a pattern colloquially called "pre pooping your pants" which would help in this specific case (http://cglab.ca/~abeinges/blah/everyone-poops/), but not in general. Writing unsafe code in Rust involves careful bookkeeping if you want to be sure the code is safe; and that goes out the window when the rug can be pulled out from underneath you any time.

Re:Ill-Advised C++ Rant, Part 2 by cousin_it on Tuesday March 08, 2016 @06:43AM (#11244707)
Well, that example doesn't use catch, so it doesn't really tip the scales on catchable vs uncatchable exceptions. It does show that you have to be extra careful when making function calls from unsafe blocks, and that destructors are especially error prone. I agree with all that :-)
Re:Ill-Advised C++ Rant, Part 2 by Manishearth on Tuesday March 08, 2016 @08:09AM (#11244954)
Recover is basically catch, it shows how a panic can leave something in a invalid and unsafe state, which can be accessed after recovery (without recover you wouldn't be able to access it). :)
Re:Ill-Advised C++ Rant, Part 2 by cousin_it on Tuesday March 08, 2016 @08:55AM (#11245194)
You can see the invalid state even without catch/recover, e.g. from destructors that are called during unwinding.
Re:Ill-Advised C++ Rant, Part 2 by Manishearth on Tuesday March 08, 2016 @01:48PM (#11247479)
Ah, I see. Right, that works in this case, but it doesn't work in all -- you can come up with a situation where the destructor is fine but the recovery can lead to arbitrary unsafe things.
Re:Ill-Advised C++ Rant, Part 2 by kibwen on Monday March 07, 2016 @01:48AM (#11237500)
dbaupp's gotten here first, but let me add my own. :)

  > Then there's the "try!" macro, which generates an 
  > invisible return on error.
You can't criticize `try!` for this in one breath and then go on to suggest exceptions in the next, considering that exceptions insert invisible returns into your entire call stack. And an exception in C++ can be thrown on nearly any operation imaginable (even assignment!); meanwhile, `try!` is explicit, and a function that uses it won't even compile unless its signature explicitly returns a Result.