Is the ? operator prefix or suffix in EK9?
← Operators and Expressions · Ref: Q936
The ? operator is always SUFFIX in EK9. Write name? not ?name.
SUFFIX SYNTAX
if userName? stdout.println(userName)
The ? comes after the variable, like Rust's .is_some() or Swift's optional unwrapping.
WHY SUFFIX
Suffix reads naturally as a question: 'is userName set?' The ? asks a question about the thing to its left. Prefix ? would be ambiguous with other operators.
USAGE CONTEXTS
- `variable?` asks: is this variable set?
- `list?` asks: is this list set? (always true for created lists)
- `result?` asks: is this result set?
- `if connection?` is a guard: only enter if connection is set
COMBINING WITH NOT
if not userName? stdout.println("No user name")
The pattern 'not variable?' reads as 'not (variable is set)'.
See Q877 for isSet details. See Q898 for more ? examples. See Q29 for tri-state. See Q2253 for why '+=' or '++' can never give an unset value a value (E08096).
Example
#!ek9 defines module qa.operators.issetsuffix defines program IsSetSuffixDemo() stdout <- Stdout() //Unset variable — ? returns false greeting <- String() stdout.println(`Greeting is set: ${greeting?}`) //Give it a value (known to be unset here, so ':='), then the suffix check again greeting := "Hello" stdout.println(`Greeting is set: ${greeting?} - ${greeting}`) //Works on any type score <- Integer() stdout.println(`Score is set: ${score?}`) score := 42 stdout.println(`Score is set: ${score?} - ${score}`)
Other ways to ask this
- Where does the ? go — before or after the variable name?
- How do I write an isSet check correctly in EK9?
- Is it ?name or name? in EK9?
Coming from another language?
EK9 uses value? as a suffix — append ? after the variable name to check if it is set. Returns Boolean, no parentheses needed.
Keywords: check, isset, mark, operator, position, question, set, suffix