Skip to content

Null coalescing operator #104

@georgejecook

Description

@georgejecook

Simlplifies replacing invalid values in expressions.

The form is:
Consequent ?? Alternate

If Consequent resolves to invalid, then the alternate is executed and returned.

Note, just like with ternary, we have to ensure that the code for alternate is not executed, and that any evaluations required to facilitate the transpiled code DO NOT execute any potentially mutating code.

The following bs
a = user ?? new User("defaultName", m.itemId)

becomes

a = bslib.coalesce(user, {
    "m": m
  }, new function(scope)
    m = scope.m
    return new User("defaultName", m.itemId)
  end sub)
function bslib_coalesce(consequentValue, scope, alternateFunc)
    if alternate <> invalid
        return return consequentValue
    else
        return alternateFunc(scope)
    end if
end function

Metadata

Metadata

Assignees

Labels

New Language FeatureA proposal for a new feature to be added to the BrighterScript language

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions