3 ms·
I'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.
by johnfn 1y ago
I'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...