E01092: '?' tests whether a value is set - an assignment target cannot carry it
What went wrong
A '?' sits between a variable and an assignment: 'label? := value'. '?' is the isSet operator - 'label?' TESTS whether label has a value. A test is not something a value can be assigned to, so the target of the assignment is the variable itself.
Worked examples
Compilable examples demonstrating this error and the correct fix:
- ek9 -q 79 (What is the assign-if-unset operator :=? in EK9?)
- ek9 -q 1039 (When do I use <- and when do I use :=? to set a variable safely?)
How to fix it
Assign to the variable itself:
- label? := value --> label := value
To assign ONLY while the variable has no value yet, use ':=?':
- label? := value --> label :=? value
To test it first, put the '?' in the condition:
- if label?
Common causes
- Meaning "assign if unset" - that is ':=?'
- Carrying 'x ??= y' or 'x?.y = z' habits from other languages
- Reading the '?' of a declaration ('name as String?') as part of the name
See also
- E01085: Suffix operator used in prefix position
- E07300: '?' where it is not needed - on a parameter, or beside a value
- https://ek9.io/operators.html
- https://ek9.io/basics.html#tri-state
Examples
Every example is compiled by the EK9 compiler: the first fails exactly as captioned, the others fix it.
Fails with E01092: '?' tests whether a value is set - an assignment target cannot carry it
#!ek9 defines module error.e01092.bad defines function labelled() <- label as String? label? := "first"
Fixed: Assign to the variable itself; ':=?' assigns only while it has no value
#!ek9 defines module error.e01092.good defines function labelled() <- label as String? label :=? "first"