Skip to content

Create syntax-nullish-coalescing-assignment.md#220

Open
consultart-artem wants to merge 1 commit into
luau-lang:masterfrom
consultart-artem:master
Open

Create syntax-nullish-coalescing-assignment.md#220
consultart-artem wants to merge 1 commit into
luau-lang:masterfrom
consultart-artem:master

Conversation

@consultart-artem

Copy link
Copy Markdown

Proposes adding ??= as a compound assignment operator that assigns only when the left-hand side is nil. Fills a gap in the existing compound assignment operator set (+=, -=, etc.). Addresses the common pattern of default value assignment where the or shorthand is unsafe for boolean values and the correct nil check form requires writing the variable path twice.

Proposes adding ??= as a compound assignment operator that assigns
only when the left-hand side is nil. Fills a gap in the existing
compound assignment operator set (+=, -=, etc.). Addresses the
common pattern of default value assignment where the or shorthand
is unsafe for boolean values and the correct nil check form
requires writing the variable path twice.

```lua
config.Enabled = config.Enabled == nil and true or config.Enabled
```

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in Luau, this would actually be written

config.Enabled = if config.Enabled == nil then true else config.Enabled

which does still repeat config.Enabled, of course, but it feels very weird to use a programming pattern we do not recommend for conditional code in an RFC.

@gaymeowing

Copy link
Copy Markdown
Contributor

Is there a reason its ??= and not ?=? I would assume because of lookahead, but this isn't stated in the RFC.

@Bottersnike

Copy link
Copy Markdown

Will this run into the same problems safe navigation did? That is to say that Roblox userdata error on missing fields, rather than returning nil, making part.Foo ??= "bar" error in a surprising way?

To potentially answer my own question, I think it might be more okay than safe navigation ended up, because part.Foo = "bar" would error anyway, so the scope for problematic code is drastically reduced.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants