500 points · by syrusakbary · 8y ago · 137 comments
Also, its amazing how fast this has popped up. Wasm was added to the compiler literally a month ago and already there is a standard library from dom manipulation and front-end frameworks. 2018 is definitely going to be interesting
On the other hand, in Rust, we have the compiler on our side. If one grabs a mutable reference to the state (&mut Model in the update function in https://github.com/DenisKolodin/yew/blob/master/examples/cou...), then the Rust compiler ensures that there are no other references to that Model. Thus, when the framework does `nextState = mutate(¤tState)`, one is guaranteed that there are no existing references to `¤tState`, and thus it is impossible for a shadow update to occur.
IMO, this is better not primarily for performance reasons, but because it is conceptually easier to mutate a state object instead of using reducers. (I haven't looked into this framework much, so I don't presume to speak about it's internal implementation, so this may be off in some framework-specific details.)
Maybe Haskell goes too far in this regard, but Rust is in line with the same way of thinking, because mutability is surfaced and checked at type level.
Debugging and rewind/replaying application state is also greatly simplified by keeping states immutable.
Having the state mutable would also need some kind of locking between the render view and updateState method
That's right, but that's exactly the main strength of Rust : the borrow checker deals with that locking at compile time with zero runtime overhead.
Not arguing on the other points though.
You may want to brush up on Rust, the language has been designed explicitly to deal with these kinds of situations.
In theory there would be no difference between mutating an existing memory area and allocating new memory to write to it. The number of bytes written to RAM are the same.
I think frameworks like React should explicitly demand that state and props will be referentially compared to optimize the rendering.
so you mean bloody expensive ? seriously, if everybody thinks like you no surprise the current web crawls on beastly computers.
"Immutable" data structures are not just objects that never get mutated, they're different manners of organizing the bytes.
This kind of performance optimization, especially outside of game engines seem unnecessary.
I have used a lot of redux in js and written a few things in Elm, and the immutible property of application state is very central in both, and an important part of being able to reason with the ui, as well as events happening concurrently.
The problem with shared mutable state might appear to be then “mutable” but it’s the combination of shared+mutable.
Ownership removes one (if it’s mutable it’s not shared). Immutability removes the other.
The outcome is the same: you can reason about your code, including concurrent code.
The price of ownership is some extra mental overhead. The price of immutability is some extra copying and cumbersome updates.
In JS/Elm you have no choice but to manage shared mutable state using pure functions and immutable data. In rust you can use either model.
Nothing against JavaScript, but I is nice that other languages can now be first class citizens on the web as well.
E.G: QT used from Python with PyQT.
It is not only GraphQL itself I had in mind though. It is also about implementing a client library similar to Apollo GraphQL where you can provide watch queries wrapping your UI components. Watch queries provide great developer ergonomics for handling the flux data flow, that is, whenever there is a change to your data in store, if the corresponding elements in datastore are watched by a query, the UI components wrapped by this watch query are re-rendered with new data, automatically.
I think this combination will help desktop development by helping to reduce boilerplate code and by providing a convention to development team. We enjoy this combination already in web UI development for the last one year, in our team.
- build views without being blocked waiting for a new rest endpoint
- get exactly what the UI needs to render
[edit]
Although to negate my own first point, nb4 being blocked on waiting for new mutations and queries :P
In that regard, rust is actually behind. Async/await is still coming.
This enables you to transparently move data-source around.
Little did they know that web-assembly was already scheming their demise.
p.s asking as @spicyj is, from her profile, an "eng manager of React @ Facebook".
Remember you can also have a polyfill (at a performance cost when parsing).
but i thought wasm couldn't manipulate the dom? is that not the case anymore?
Using such bridges, you can create anything you can with Javascript in any language that compiles to webassembly.
The story with Rust compiling to it is still younger; it works well at the moment, but is very fiddly. The early adopters and enthusiasts are working on making it all smoother. So for most people, it's still a bit young. Just depends on your temperament.
DOM support for wasm really means native DOM support, which removes that layer of indirection, and therefore will make it faster.
https://news.ycombinator.com/item?id=13614886
claims it is but it's no longer in the high level goals in the spec link is dead... so do you know if it is?
Everyone I talk to suggests that it will be sometime next year.
I understand JS is popular to hate, for a variety of reasons, but those reasons seem mostly to boil down to nerd cred. The "hate what's popular because being contrarian means I'm cool" crowd.
So, and this is an honest question here, why should I invest the time to learn an entirely new syntax for a framework to build web applications? What can this do that vue.js or react cannot? (And I don't mean it does it differently I'm looking for it does it better or JS framework doesn't do it.)
Then you're not paying attention. I'm not sure how you expect anyone to answer your "honest question" when you've already dismissed all the answers. It's not just fashion, programming language design is a real thing that it's possible to do badly or well, Javascript is a legitimately bad language, and Rust is a legitimately good language.
Learn Rust or another ML-family language, actually learn it to the point where you can write a decent, full-sized, idiomatic program in it. It won't be easy, but it's worth it. I mean, I could talk through all the reasons such languages are better, but it seems like you're already determined to dismiss them, so really the only way is to see for yourself.
Yeah, statements like these without even trying to back them up are pretty ridiculous.
Sometimes someone brings some stupid issue that's never a problem in practice, but most of the time people just admit they simply have no real experience with JS. They just love to claim how superior they are, and anyone who disagrees is "not paying attention".
FYI you are just showing how awful are communities around "legitimately good" languages.
Just to let you know Rust's community is pretty cool and not awful at all. For instance the Rust Survey 2017 reports that 98.7% of respondents feel welcome within the Rust Community.
Furthermore according to the Stackoverflow 2017 survey, Rust is the "most loved" language.
Could You elaborate?
Non-lexical scoping is awful, everyone knows it's awful. Recent versions added a better-scoped "let" which is progress in a way but now means you have two different kinds of declarations with very different kinds of scoping. "this" in Javascript is just entirely useless, confusing semantics that resemble no other remotely mainstream language. "Prototypal inheritance" is a mistake, again no other mainstream language uses it for good reason, and again newer versions of Javascript have added "class" to correct this but that leaves the language with two radically different ways of accomplishing the same thing. "undefined" manages to be an even worse version of the billion-dollar mistake, null; errors become apparent even further from where they occur ("foo is not a property of undefined" failures a long way away from whatever caused the value to be undefined), where what you want is fail-fast. Extremely loose value coercion at runtime is terrible for large programs in a language without a type system; even at the time Javascript was first created, Python or TCL knew this. (Perl didn't, but avoided the worst of the problems by not having so much operator overloading, e.g. using . for string concatenation). Indeed lack of any kind of type system is pretty indefensible in a general-purpose language these days, even Python's getting type hinting, though this was less common knowledge back when Javascript first came out. The language is far too dynamic for working with mixed security contexts even though it's the most popular language for that very use case, forcing browsers to resort to crude sandboxes and coarse-grained permissioning instead (e.g. you can give a site access to your webcam/microphone, at which point all the ads running on the page have access too). And there are a bunch of small usability niggles (the "wats" you see talked about), none of which is important in isolation, but developer-friendliness does add up.
Rust is a pretty conservative ML-family language design: where it differs from OCaml it's because of deliberate design decisions. (In fairness some of Javascript is descended from Smalltalk which was more respected at the time than it is now). Really most of what Rust does ought to be table stakes for language design (and it kind of is, looking at e.g. Swift or even Kotlin), should have been since the 1970s when ML came out: to quote Wikipedia, "Features of ML include a call-by-value evaluation strategy, first-class functions, automatic memory management through garbage collection, parametric polymorphism, static typing, type inference, algebraic data types, pattern matching, and exception handling. ML uses static scoping rules." (Rust may appear not to have exceptions, but ML "exceptions" have more in common with Rust's panic/recover than with Java-style exceptions). These things are small and simple but very useful and general, and allow you to push a lot of work out into libraries written in plain old "userspace" Rust code (though this would be much more true if the language had HKT, grumble grumble) rather than having to "bolt on" ad-hoc features at the language level. The only novel-for-mainstream language-level feature in Rust is its ownership tracking (i.e. the borrow checker), which is about the right amount of innovation for a programming language to have, and by all accounts is working well. Good language design is as much about leaving things out (of the language itself, pushing them into userspace code) as it is about putting things in.
From my experience main problems seem to be:
1) poor static analysis of code, leading to bad Intellisense etc. - Mostly solved by TypeScript.
2) DOM manipulation APIs. I don't see how Rust will help here. Especially considering JS frameworks solve this. I guess that's what's new in the OP.
Otherwise JS serves as pretty good language for beginners/UI wiring.
But thanks for at least describing some strengths (and weaknesses) of Rust when directly compared with JS.
Typescript is not Javascript. Most people would agree that that Typescript is a reasonable language.
The fact that it can be transpiled to Javascript is irrelevant, many languages can be transpiled to Javascript.
It allows you to write your web app in Rust.
>And I don't mean it does it differently I'm looking for it does it better or JS framework doesn't do it.
"Better" depends on the use case, so merely by being different something might better fit another use case.
("scripting" in quotes because of how rust embedded in the html templates can be so much more than logic-less templates- a bit like a server side template, the dom, and clientside javascript all in one- and I'm just guessing, but because it's not dynamic, but compiled, nonetheless safe even though logic is in there...)