4 ms·
Thanks! I should have clarified a bit better that example. The point I was trying to make is that Rust's ownership model would allow you to design an api where
by bkolobara 1y ago
Thanks! I should have clarified a bit better that example.
The point I was trying to make is that Rust's ownership model would allow you to design an api where calling `window.set_href('/foo')` would take ownership of `window`. So you would not be able to call it twice. This possibility doesn't exist at all in TypeScript, because it doesn't track lifetimes.
Of course, TypeScript can't do anything here either way. Even if it had knowledge of lifetimes, the JavaScript API already existed before and it would not be possible to introduce an ownership model on top of it, because there are just too many global variables and APIs.
I wanted more to demonstrate how Rust's whole set of features neatly fits together and that it would be hard to get the same guarantees with "just types".
- johnfn 1y agoI'm not as familiar with Rust, but isn't there still a gap? For instance, if we modified window.set_href to have move semantics, wouldn't this still work (i.e. not produce an error)? let win = window.set_href("/foo") win.set_href("/bar") You might say "why would you ever do that" but my point is that if it's really the lack of move semantics that cause this problem (not the deferred update), then you should never be able to cause an issue if you get the types correct. And if you do have deferred updates, maybe you do want to do something after set_href, like send analytics in a finally() block. In fact, Typescript does have a way to solve this problem - just make `setHref` return never[1]! Then subsequent calls to `setHref`, or in fact anything else at all, will be an error. If I understand correctly, this is similar to how `!` works in Rust. So maybe TS is not so bad after all :) [1]: https://www.typescriptlang.org/play/?ssl=9&ssc=1&pln=9&pc=2#code/GYVwdgxgLglg9mABGAhgNxgcxVApgChACcAbALkQGcoiYxMBKCsXNXIxAbwChFEB3OgBM4-AHQk4EHPDBiAFkVzBEAXkTESAbl6Ioi0clz9EAUSJE4RfACIAqmCUoI8lACMSuGwy2IA9H6IAFYg1HpwVDKUwACeiAAqAMrcAL7c3BAIYXhh6vgMagB8XLp8qBjYePgA5CiUQsDVPqXI6Fg4BNVC7I3NKUA https://www.typescriptlang.org/play/?ssl=9&ssc=1&pln=9&pc=2#...
- bkolobara 1y agoYour Rust example would not work, because `window.set_href("/foo")` would not return anything (it would return the unit type "()" aka void). And you can't call `set_href()` again on "()". This is a common pattern in Rust, allow certain functions to only be called once on specific objects. I really like your TypeScript solution! This actually perfectly solves the issue. I just wish that this was the ONLY way to actually do it, so I would not have experience the issue in the first place.
- johnfn 1y agoThanks for the explanation! That's a very nice pattern. > I just wish that this was the ONLY way to actually do it I completely agree.
- norman784 1y agoJust to add what the other comment said, take a look and run these two examples to see the errors you could get: https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=d2aeb88d1ccf300eb1f53ef69d1a850b https://play.rust-lang.org/?version=stable&mode=debug&editio... https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=6eeb07e395d638805842a176a354d8d3 https://play.rust-lang.org/?version=stable&mode=debug&editio...