expr icon indicating copy to clipboard operation
expr copied to clipboard

Optional Chaining on Function Calls / Methods

Open xiaoas opened this issue 1 year ago • 0 comments

Currently, Optional chaining on Object fields work well:

MyObj?.Field1 ?? "default"

However, one cannot make an optional call to a method on an object:

// this panics when MyObj is nil
MyObj?.Method1() ?? "default"
// this does not compile
MyObj?.Method1?.() ?? "default"

So one is forced to write this, which works but is more verbose. Especially when MyObj is actually a long expression:

MyObj != nil ? MyObj.Method1() : "default"

Expected Behavior

?.() should be a valid optional function call syntax

xiaoas avatar Dec 18 '24 02:12 xiaoas