←back to thread

160 points leontrolski | 3 comments | | HN request time: 0.001s | source
1. bbb651 ◴[] No.41887862[source]
You can almost do it with `condition and value`, but you get `False` instead of `None` (and adding any more code makes this worse than `value if condition else None`. Interestingly lua like `<condition> and <true-expression> or <false-expression>` ternaries actually work in python, with the added footgun that <true-expression> must be truthy and people will despise you).

Rust for example has a solution for this in std, there's a `bool::and_some` method (and `bool::then` that takes a closure to avoid eagerly evaluating the value), but `if { ... }` isn't an expression like `if { ... } else { ... }` is, probably to avoid coupling the language to `Option`.

replies(1): >>41888024 #
2. steveklabnik ◴[] No.41888024[source]
Option is already a lang item, so that would t be an issue. I don’t know what the actual underlying reason is, or if it was ever even proposed.
replies(1): >>41890649 #
3. bbb651 ◴[] No.41890649[source]
I meant more than it already is, I referenced this: https://stackoverflow.com/a/43339003. I do think it would be neat but it's niche enough to not be worth the non-obvious behavior of boxing with `Option`, e.g. you might forget an `else { ... }` and get complex type errors.