jakt icon indicating copy to clipboard operation
jakt copied to clipboard

Optional chaining

Open awesomekling opened this issue 2 years ago • 2 comments

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>.

awesomekling avatar May 19 '22 15:05 awesomekling

i'm interested in picking this one up.

brian-gavin avatar May 24 '22 03:05 brian-gavin

[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().

wlib avatar Jul 25 '22 22:07 wlib

Implemented in dc4e06920f30dc9b27f54bca04f5aa4b9d6cc867.

alimpfard avatar Aug 03 '22 09:08 alimpfard