null assignment operator added
This commit is contained in:
1 parent
8cd980514b
commit
555c9b94de
11 files changed
+169
-9
No files matched your search
+13
-7
@@ -188,14 +188,13 @@ Note that assignment operator returns rvalue, it can't be assigned.
|
||||
## Modifying arithmetics
|
||||
|
||||
There is a set of assigning operations: `+=`, `-=`, `*=`, `/=` and even `%=`.
|
||||
There is also a special null-aware assignment operator `?=`: it performs the assignment only if the lvalue is `null`.
|
||||
|
||||
var x = 5
|
||||
assert( 25 == (x*=5) )
|
||||
assert( 25 == x)
|
||||
assert( 24 == (x-=1) )
|
||||
assert( 12 == (x/=2) )
|
||||
x
|
||||
>>> 12
|
||||
var x = null
|
||||
x ?= 10
|
||||
assertEquals(10, x)
|
||||
x ?= 20
|
||||
assertEquals(10, x)
|
||||
|
||||
Notice the parentheses here: the assignment has low priority!
|
||||
|
||||
@@ -248,6 +247,13 @@ There is also "elvis operator", null-coalesce infix operator '?:' that returns r
|
||||
null ?: "nothing"
|
||||
>>> "nothing"
|
||||
|
||||
There is also a null-aware assignment operator `?=`, which assigns a value only if the target is `null`:
|
||||
|
||||
var config = null
|
||||
config ?= { port: 8080 }
|
||||
config ?= { port: 9000 } // no-op, config is already not null
|
||||
assertEquals(8080, config.port)
|
||||
|
||||
## Utility functions
|
||||
|
||||
The following functions simplify nullable values processing and
|
||||
|
||||
@@ -139,6 +139,21 @@ var name by Observable("initial") { n, old, new ->
|
||||
|
||||
The system features a unified interface (`getValue`, `setValue`, `invoke`) and a `bind` hook for initialization-time validation and configuration. See the [Delegation Guide](delegation.md) for more.
|
||||
|
||||
### Assign-if-null Operator (`?=`)
|
||||
The new `?=` operator provides a concise way to assign a value only if the target is `null`. It is especially useful for setting default values or lazy initialization.
|
||||
|
||||
```lyng
|
||||
var x = null
|
||||
x ?= 42 // x is now 42
|
||||
x ?= 100 // x remains 42 (not null)
|
||||
|
||||
// Works with properties and index access
|
||||
config.port ?= 8080
|
||||
settings["theme"] ?= "dark"
|
||||
```
|
||||
|
||||
The operator returns the final value of the receiver (the original value if it was not `null`, or the new value if the assignment occurred).
|
||||
|
||||
## Tooling and Infrastructure
|
||||
|
||||
### CLI: Formatting Command
|
||||
|
||||
Reference in new issue
Block a user