Properties¶
Computed properties provide variable-like syntax backed by custom get/set logic.
Read-Only Property¶
Read-Write Property¶
let mut _value = 10
let Value with
get () = _value
and set (v) = _value <- v
print Value
Value <- 42
print Value
# => 1042
Multi-Line Getter¶
Property bodies support multiple expressions, just like function bodies. Indent the body further than the let keyword.
Multi-Line Setter¶
let mut _a = 0
let mut _b = 0
let SetBoth with
set (v) =
_a <- v
_b <- v * 2
SetBoth <- 5
print _a
print _b
# => 510
Multi-Line Getter and Setter¶
let mut _val = 0
let Val with
get () =
let r = _val
r
and set (v) =
_val <- v
Val <- 99
print Val
# => 99
with on Next Line¶
The with keyword can appear on the line after the property name:
Conditional Getter¶
Built-In Dot Properties¶
Endo provides built-in dot properties on common types:
# List length
let xs = [1; 2; 3]
println xs.length
# => 3
# Option queries
let x = Some 42
println x.isSome
println x.isNone
# => true
# => false
# Tuple access
let t = (10, 20)
println t.0
println t.1
# => 10
# => 20
Key Techniques¶
- Single-line bodies go directly after
=:get () = expr - Multi-line bodies start on the next line, indented past the
letkeyword - Use
andto combine get and set accessors in either order - Write-only properties omit the getter; read-only properties omit the setter
- Properties are accessed like variables: read with the name, write with
<-
See Also¶
- Variables & Bindings — property syntax reference (section 4.3.1)
- Type System — built-in dot properties on tuples, options, results
- Lists & Collections — list and string dot properties