onflow/cadence-tools

[LS] Propose optional chaining

Closed this issue · 2 comments

Issue To Be Solved

Developers may attempt to use a field or function of an optional type directly, e.g.

struct S {
   let x: Int
   // initializer omitted
}

let s: S? = S(x: 1)
s.x  // invalid: type `S?` has no field `x`.

However, the underlying type of the optional may have the requested member.

Currently, whenever the accessed member cannot be found on a type, an error is reported.

Suggested Solution

When the type of the accessed value is an optional type, check if it has a member with the requested member and suggest optional chaining to the user.

For instance suggest to use s?.x in the example above.

@turbolent This can be closed I think. On Playground, I am getting the expected message, so it must have been implemented already

image

Yeah, it proposes the fix in the error message, but does not provide a quick fix yet.

Implemented this in onflow/cadence#2625 and #162