by bbatha · 9y ago · 0 comments
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?
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.