by Animats · 10y ago · 0 comments
That's not a survey, that's a focus group.
I'm impressed with Rust. The borrow checker is the biggest advance in memory safety since garbage collection.
But there's a lot about Rust that's unnecessarily weird.
- The type system is unusual, and complex. It's hard to do anything without templates.
- The template libraries are heavily biased towards closure-oriented functional programming. This is cool, but hard to read. Parts of expressions are nameless and have no visible type. This is terse but hard to maintain.
- The hacks needed to avoid the need for exceptions are uglier than exceptions.
This creates major obstacles to adoption. Unlike the borrow checker, none of these things are clearly improvements. They're just different.
I would have gone for Python-type exceptions rather than error enums and macros, single-inheritance OOP rather than traits, and Python-type with clauses rather than RAII.
You need generics to have the borrow checker that you praise above. Otherwise references would be basically crippled.
> The template libraries are heavily biased towards closure-oriented functional programming.
No, they aren't. I use for loops all the time in Rust, and it's totally natural to do so. My general guideline in my own code (which is followed in projects like WebRender and Servo layout) is that if it's one line, I use the functional idiom; if it's more than one line, I use a for loop.
> Parts of expressions are nameless and have no visible type. This is terse but hard to maintain.
Are you thinking of unboxed closures here? If so, they're necessary to avoid unnecessary heap allocations in normal code. If we didn't have that, then we couldn't compete with C++11 closures.
> - The hacks needed to avoid the need for exceptions are uglier than exceptions.
Unwinding is the single most divisive issue in Rust. We would alienate most of our community (I'm not kidding) if libraries required it. For a time, there was a language fork over the issue until it was resolved.
> Unlike the borrow checker, none of these things are clearly improvements. They're just different.
No, they're needed for the borrow checker and most of the other features to work.
> I would have gone for Python-type exceptions rather than error enums and macros
Then, as above, you instantly alienate all embedded developers.
> single-inheritance OOP rather than traits
This doesn't work in practice unless you introduce interfaces: it's way too inexpressive. This is why Java and C# have interfaces. Traits are just interfaces.
> Python-type with clauses rather than RAII.
You want to force an extra layer of indentation for every Box or Vec you create, and leak memory if you forget to write "with"? No thank you!
> No, they aren't.
I think that came off more combative than you meant it to :)
>> Parts of expressions are nameless and have no visible type.
>Are you thinking of unboxed closures here?
I think Animats is complaining about the functional style in general, which is at least more common in Rust than in C++ or Python. And if that's right, it's a complaint I mostly agree with. Functional programming can cram quite a bit into one line, and that's all well and good, but a lot of the savings comes from omitting the variable names that are usually there in imperative code, and some clarity gets lost when you don't name things.
I agree with your rule of thumb. Functional stuff is great when what it's doing is obvious.
Are you talking about point-free style? Point-free style is rare in Rust, partially by convention, partially due to the fact that references and values are different and often you need to wrap in a closure to convert one to the other.
No, no, Python like with clauses, for opening files and such.
Presumably a data type that contains a "with resource" would itself have to be a "with resource" (or else why bother?). This makes "with"-ness abstraction-piercing and infectious, so, for backwards-compatibility, if there was any chance of a type eventually containing a "with resource", the programmer would have to mark the type as such a resource preemptively, or else be forced to make a breaking change to their library. All for a minor syntactic difference in how one constructs values.
Also, how does one return values from `with` clauses? It's useful to be able to write a function that returns a file, so one would need yet more machinery, just to be able to cancel the normal with-cleanup. Again, all for a relatively minor syntactic distinction.
On languages that support currying, or leaving the last parameter out of the call if it is a lambda, you can create high-order-functions for resource management.
The effort is no different than implementing RAII classes.
withDBConnection {
r = db.execute ("SELECT....")
}
Here the function withDBConnection calls the lambda, providing it an implicit parameter for the db connection, which it fully controls.There are more fancy ways to improve on this, specially if the language also allows for macros.
So I find a bit sad, that the discussions of with/using/try don't focus on the FP way of doing resource management.
You mean generics not templates?
Nothing forces you to use generics, you can pass traits by pointer and avoid the code generation. It's actually a really nice property that lets you determine static vs dynamic dispatch independent of implementation
WRT Exceptions, this has been widely debated. I personally prefer error codes. I hate unchecked exceptions, ruins the whole idea.
Also I'm not a huge fan of OOP hierarchies. They tend lean more towards Is-A vs Has-A when the latter is more common across the domains I've worked in.
I felt like this when I read the scary documentation: https://doc.rust-lang.org/book/error-handling.html
However, in the total 4 hours or so that I have been programming in rust the way of handling errors seems very sensible.
- Match expressions instead of "if (error) dosomething()"
- Match expressions with destructuring gives the error message.
- Match expressions are exhaustive which forces errors to be checked.
Its simple and elegant. I think the documentation is a little overwhelming when it comes to error checking and introduces non-essential craziness without warning.
Like what? (Disclaimer: I originally wrote that chapter as a blog post[1], so I'm curious to hear what I could have cut out. I tried to find things to cut, but given my target audience, I couldn't find anything substantial.)
Here would be my suggestions, I hope they are useful:
1. Its about error handling. Options are conceptually similar but they're not error handling. Options can go somewhere else, maybe a previous chapter.
2. From what I understand from my almost zero experience of rust, the match/deconstruct with a Result type will do 99% of error handling -- even though it might be verbose or ugly to some. Its also the basis for the combinators. This can be the short and simple error handling chapter.
3. The combinators are nice constructs to make code more linear looking and are convenient when working with the std library. If they are stated as an optional convenience then they become less scary. Error handling is so fundamental that I should not have to learn about combinators to do it.
What I suggest is a rearrangement of the material. The documentation itself is clear -- its just too much.
With that said, in a book, I totally agree that the chapter could be broken up into more manageable pieces. But I do think all of the content there is essential for understanding how to do error handling in Rust. I feel pretty safe in saying that I use all of those concepts in most crates I've published.
Thank you for replying by the way. I'll continue to noodle on your thoughts and see if there's a happy place between us. (I kind of suspect that happy place is the new book, with content delivered in more manageable chunks. Alas, my blog is not book. :P)
[1] - http://rust-lang.github.io/book/ch09-00-error-handling.html
If you don't put in exceptions, you seem to end up re-inventing "longjmp".
Their purpose is to recover from panics when embedding Rust in code written in other languages, where you don't want the panic to try and cross the ffi boundary (or to stop applications that should never crash from crashing at the toplevel, or for better reporting before a crash). Many FFI-based applications turn panics into aborts anyway. Firefox does.
The unwind-recovery functionality in Rust is just enough to get you these useful features if you need them (zero cost if you don't; turn them into aborts), without needing a full on exception system. The reason Rust avoids an exception system isn't the cost of unwinding, it's because it considers exceptions to be an inferior pattern compared to return value based / monadic error handling.
Rust doesn't have a mailing list anymore. I haven't seen much annoyance with panic recovery. There used to be some with panics in general because in the past they couldn't be turned off or recovered from without a new thread, but this has been addressed for ages with panic=abort and recovery. What the hell are you talking about?
Do you mean the generic types and functions that stop you having to reimplement a whole pile of common patterns and data types yourself? You're welcome to write your own VecInt and VecString and VecMyStruct and VecYourStruct and OptionInt and HashMapIntString, but I'll go out on a limb and say that would be more annoying than <>'s in a few places. For one, not having generics would make it far more annoying to create reusable zero-cost abstractions around `unsafe` code, resulting in your other favourite criticism: more unsafe code for "unnecessary" (i.e. performance) reasons.
> - The template libraries are heavily biased towards closure-oriented functional programming. This is cool, but hard to read. Parts of expressions are nameless and have no visible type. This is terse but hard to maintain.
This isn't true: most generic types go via other schemes, and only use closures for simple combinators (making one line manipulations simpler and easier to read, but like with anything, can be abused), or when they're required to achieve the desired semantics (iterator pipelines).
---
In any case, (some of) your proposed replacements don't make sense as the only built-in for their given functionality, for a systems language. If the task in question can take the loss of control, then Python (or similar) seems like a better choice for you.
> would have gone for Python-type exceptions rather than error enums and macros,
True Python-style exceptions require allocations to handle errors.
> single-inheritance OOP rather than traits
This forces dynamic dispatch everywhere and loses type safety (when passing types through functions and storing them in data structures), rather than being able to specialise functions and data types, and track exact types through code.
Also, only offering single-inheritance has significant semantic problems, e.g. what happens when your type is both equatable and cloneable?
> Python-type with clauses rather than RAII
What exactly does this address? It seems just like a syntactic rephrasing of `let x = foo()`, given it would have to be compulsory for certain types, or else guarantees are lost.
---
Rust is, as you say, different to other languages, but very little of it (and none of the features you call out) was chosen just to be gratuitously different. The new features of course might just take a little time to get used to if one is coming from mainstream imperative/OOP languages.
For example try writing even a basic function accepting two scalars of type T and U and performing arithmetic on them. The way the Rust compiler works it isn't possible without specifying various bounds on the traits (which I appreciate, actually, for explicitness). And even then there are limitations around casting that require non-trivial workarounds (it is ridiculous, frankly, that the num crate isn't in the standard library, but that crate has its own problems).
And there is no specialization, which is a severe limitation.
Can't disagree with your comments re: python.
> The way the Rust compiler works it isn't possible without specifying various bounds on the traits.
This is an explicit choice: C++-style check-at-use-site results in some seriously ridiculous error messages, and there's no boundary between implementation and signature, meaning a change to some small detail (and even syntax, in C++) in the implementation can accidentally cause downstream code to fail to compile. It is definitely annoying some times, but Rust, and most other languages with some sort of templates/generics, thinks the benefits outweigh the cost.
> And there is no specialization, which is a severe limitation.
Indeed, it is a limitation that the team recognises and is working on: https://github.com/rust-lang/rust/issues/31844 (Parts of which are already available in the nightly releases.)
That's a feature! It works the same way in every language other than C++ or D that implements generics.
I much prefer explicitly typed generics to untyped generics as in C++. Not only are the error messages far more comprehensible, but it lets us avoid having to introduce confusing name resolution rules like ADL.
> And there is no specialization, which is a severe limitation.
There is now [1].
Also, huzzah for specialization. I'll try that out asap. Thanks for the heads up.
Some people want nice, simple, dense multidimensional arrays. Some want the ability to extract a square section from an array as a subarray. Some want that for N dimensions. Some don't want the overhead for that generality slowing down their matrix multiply. Some are content with arrays of arrays. Some want ragged arrays (rows not the same length). Some want arrays in a format a GPU can use.
Passing the buck to each package, though, results in recopying arrays as matrices move from a function in one package to a function in another. One reason people use MATLAB, FORTRAN, and Python's NumPy is that they at least have a standard representation and matrix functions interoperate. I'd argue for supporting dense arrays in the language (and the optimizer) and doing the fancy stuff in packages.
Personally, I find error enums with macros, traits over traditional OOP and RAII to be strictly positive features, and all three are among the (quite numerous) reasons that I love Rust
This is the worst side of this place.
Rust usage is very low. It isn't even in the TIOBE Top 20 for 2016.[1] (In the detailed listings, it's at position 46, just above Awk.) Why is market penetration so low? I'm arguing that there's too much gratuitous new stuff for Rust to be acceptable to the C/C++ community.
I think that the simple fact is that ownership, lifetimes, and borrowing are unfamiliar. They have a learning curve, period. We can't expect a language with them to take over the world in a year. Programming languages are a mature market. Progress will be slow.
Rust is doing far better than 95% of new programming languages after a year. Most programming languages are dead after a year.
FWIW, I think a few explainer pieces would help bring about more enlightened discussions. For example, there is a lot of bikeshedding around Rusts' decision to wrap overflows and potential future improvements. But the rationale and interesting technical challenges each alternative brings is scattered across blog posts, forum threads, and Github issue tickets.
1: http://huonw.github.io/blog/2016/04/myths-and-legends-about-...