4 ms·
I can't wait for more languages to adopt the "?" operator [1] like the Rust one. It's just syntactic sugar for "if expr null return null" but makes it far easie
by aziis98 3y ago
I can't wait for more languages to adopt the "?" operator [1] like the Rust one. It's just syntactic sugar for "if expr null return null" but makes it far easier to write code in a more monadic style.
(mostly waiting for this in JS and Go)
[1]: https://doc.rust-lang.org/reference/expressions/operator-expr.html#the-question-mark-operator https://doc.rust-lang.org/reference/expressions/operator-exp...
- fernandotakai 3y agoi remember using this back in my groovy days https://groovy-lang.org/operators.html#_safe_navigation_operator https://groovy-lang.org/operators.html#_safe_navigation_oper...
- madeofpalk 3y agoGo actually needs proper nil/null/optionality first before it can get syntax features to deal with them.
- mrkeen 3y agoI can't wait for more languages to simply not include null at all. It makes trying to spot them in static analysis and runtime checks much easier, because you no longer need either.
- kibwen 3y agoIn the context of the parent's example, Rust doesn't include null at all, it just has a standard Option type with a None variant. While the other mentioned languages (JS and Go) do have null, they don't necessarily need to remove it to start getting the benefits, they just need to provide standardized alternatives and get the community to follow along (and if, say, a fancy new ? operator only worked on these new types and didn't work on null in general, that would be a strong carrot).
- ubertaco 3y agoThe example of Rust is one where no types are nullable by default, but only if you wrap them in an `Option<T>`. Given these two function signatures: pub fn getUser() -> User pub fn getMaybeUser() -> Option<User> This code won't compile, because `getUser()` cannot return a `None`: pub fn foo() -> Option<String> { let user = getUser()?; return user.name } But this code will compile: pub fn foo2() -> Option<String> { let user = getMaybeUser()?; return Some(user.name) } (rust playground link: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=8e1e919c56b1f0945abede5113f94850 https://play.rust-lang.org/?version=stable&mode=debug&editio...)