let id = node.parent()?
.parent()?
.data_get("id")?
.parse::<usize>()
.ok()?;
I'd also expect that someone will write higher level libraries so that you aren't doing raw DOM manipulation. A rust vdom wasm library would probably be blazing fast.Even regardless of performance, it would be quite nice to have! I've been meaning[1] to build a small prototype to do something clever[2] with it, but didn't have the time yet. Feel free to steal these ideas :)
[1]: https://scribbles.pascalhertleif.de/impl-virtual-dom-cli-lib... [2]: https://scribbles.pascalhertleif.de/rich-diffs-of-rendered-h...
let id = node.parent().unwrap()
.parent().unwrap()
.data_get("id").unwrap()
.parse::<usize>().unwrap();
If there is something horrible about that I don't see it.Dealing with this case properly without using a single `?`, gets really ugly with a lot of nested
if let Some(parent) = node.parent() {This could be a function to isolate just the attribute you need, but seeing the example as it is presented can be off-putting from people used to scripting languages like ruby/python, which are more concise.
? is far easier and faster to type than .unwrap().
So no, there's nothing horrible that I see, there is something horrible that I feel though.
Isn't unwrap():
a) boilerplate
b) repetitive?
In other words, it's boilerplate here because this particular operation has many possible failure modes, and Rust is pointing those out to you.
It's repetitive in this particular instance, but doesn't have to be in general.
if the node always having a parent is a invariant of your application, you should use `expect` with a clear error message when the invariant is broken.
if your node can have no parents then you want to handle this case explicitly with a `if let Some(parent) = node.parent() {` pattern.
Most of the cost of vdom is the resulting DOM ops and reflow/restyle the browser needs to do. It may be faster than the fastest today, but you're unlikely to see any kind of performance leap or even anything noticeable.
I see that there is some discussion here [1], including a suggestion to add a Haskell-style "do" block. But that's just one guy's blog post.
It sounds like there are some that would like to move Rust in the direction of becoming more Haskell/ML-like (which is to say more purely functional) rather than the current balance between imperative and functional? Is there any internal tension in that regard?
This leads to a situation where, if there were to be a monad trait, that trait should abstract over not only types, but another traits generically.
Not to speak about some other hurdles, like lifetimes. The design space is inherently more convoluted than in languages with boxed, possibly dynamically dispatched types. It may be possible, but it's not easy, and we can't copy the monads straightforwardly from Haskell or Scala.
Because people wouldn't expect it to return due to behaviour of existing languages yet that's what it does in Rust?
Many are in favor of doing it, though. I'm personally against it.
For the cleanest code, I would use `?` and a `From` impl for the error transformation.
If I didn't want to use `From`, I'd use `map_err`, `and_then`, etc. Both the Result and Option types have wonderful interfaces for chaining.
Is there a way to rename / alias methods like that?
edit: I see ? is going to be a trait you can implement to do that, so that'll be nice (probably).
If you don't care about errors, don't use Rust. Use C instead. Simple solution.
Second, most of the time you can use the ? operator to greatly simplify error handling. Any code path that shares the same error type can just pass through errors as if they were an exception. The ? operator is also aware of Into/From trait implementations so in many (or even majority of) cases you can use the ? operator even if the function you're calling has a different error type! If I remember correctly, you can even exploit the module system to provide a per-file Into/From implementation for your custom error type if it doesn't map cleanly across use cases (they just have to be in the same module as the type). Rust's zero cost abstractions and trait system make it really easy to reduce boilerplate without compromising on readibility or discoverability.
Third, the Result type has a number of functional operators for responding to and combining results. You can do Result.and_then(...) with a closure or function call to do something when there is no error and Result.or_else(...) to do something when there is an error. You can use the and or or operators to combine multiple results and there are many other utility functions that are universal to Rust error handling that make the entire experience very pleasant, composable, and readable with a little experience.
This is a highly contentious assertion, and I don't think it's true for most programs, at least not in the way you're suggesting.
Most of the point of exceptions is that errors don't have to be handled; or rather, in most application software, almost all errors should be handled high up in the stack, near the event loop or server request / response dispatcher.
The kind of error that you'd handle high up the stack is, say, a general I/O error reading a file.
But when you look up an item in a collection (or a node in a DOM tree), and it is not found, the only place where you can sensibly handle that is right where the lookup happens, usually.
In a language that doesn't follow this philosophy, what you get instead are random NullPointerExceptions etc. If you go and look at a random C# or Java project on GitHub and check its tracker, you will see that - surprise, surprise! - that is exactly the type of error that shows up most.
An item not found in a collection or tree is not an error.
Null pointer exceptions are little different.
On one hand this is really cool and potential for client-side Rust. On the other hand:
Eh, no thanks. :) If I could get a macro or shorthand for that, I'd welcome all the other benefits of a large scale client-side platform with strong typing.