Optional chaining operator

Would be nice to have an optional chaining operator (similar to JavaScript’s ?., see Optional chaining (?.) - JavaScript | MDN), essentially that navigates the same as the regular . operator, with the exception that it short-circuits and returns Blank if the left hand side expression of the operator is Blank.

Syntax could be anything, does not need to follow the JS approach (although I believe it is supported by many languages), as long as the semantics is the same.

This would enable simpler checking of blank values.

Example:

WithName(
    myList.Filter(criterionThatMayYieldNoResults).First(), 
    result, 
    If(
        result.IsNotBlank(), 
        result.field,
        "")
)

becomes

myList.Filter(criterionThatMayYieldNoResults).First()?.field

It is even more beneficial when navigating multiple layers, such as the properties of a lookup row.

Example:

WithName(
    thisRow.lookupColumn, 
    linkedRow, 
    If(
        linkedRow.IsNotBlank(), 
        WithName(
            linkedRow.nestedLookupColumn, 
            nestedLinkedRow, 
            If(
                nestedLinkedRow.IsNotBlank(), 
                nestedLinkedRow.field, 
                ""), 
"")

becomes

thisRow.lookupColumn?.nestedLookupColumn?.field

Please let me know if this is already possible, or even needed.

3 Likes

I’ve been looking for something similar for a while now. I find the If syntax to be a bit ugly when used merely to check if something is not blank.

I wrote about something similar here:

But I was ultimately dissuaded and instead I landed on:

Here’s a doc showing another place I’d like to use something like this for:

In this doc I propose the idea of IfBlankReturnOtherwiseDo operator (terrible name, but you get the idea). You would use it in your code like this:

myList.Filter(criterionThatMayYieldNoResults).First().IfBlankReturnOtherwiseDo("", CurrentValue.field)

What possible names | syntax can you think of for the ? operator?

2 Likes

I’ve read the topics you linked and I agree with the proposed changes agreed upon there. I don’t think it’s exactly the same scenario however.

Ultimately, just playing around with an example, it seems the . operator already handles optional chaining implicitly. Should have checked before making this topic :sweat_smile:

Here’s the example:

As you can see it does not complain for both approaches retrieving the nested field, as the . operator will automatically shortcut and return Blank if the LHS is Blank.