jakt
jakt copied to clipboard
Optional chaining
We should allow JavaScript-style optional chaining: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining
struct Bar {
baz: i64
}
struct Foo {
bar: Bar?
}
function main() {
let mut foo = Foo(bar: None)
let baz = foo?.bar?.baz
}
Implementation wise, this should become an expression that returns an Optional<i64>, since that's the type of the last ?.
If we short-circuit on any of the ?. operators, we should return an empty Optional<i64>.
i'm interested in picking this one up.
[not sure where to ask this]
I'm wondering if there is an even more elegant approach syntax for these different types of accesses (normal a.b, optional a?.b, and infallible a!.b).
Typically, a chain of property accesses "degrades in certainty", so writing a?.b!.c would be ambiguous. Does it mean that c must exist if b does?
Also, the most common case would be something like a!.b?.c?.d?.e?.f?.(). Would it make more sense to "carry over" the degree of certainty unless overridden, like a!.b?.c.d.e.f().
Implemented in dc4e06920f30dc9b27f54bca04f5aa4b9d6cc867.