References
Ref<T> points to a value of type T. It compiles to a Go pointer *T but is never nil.
Referencing
Section titled “Referencing”& takes a reference to a value that has an address: a variable, a field, or an element.
let user = User { name: "Alice", tags: ["admin"] }
let user_ref = &userRef<User> pointing to userlet name_ref = &user.nameRef<string> pointing to user.namelet tag_ref = &user.tags[0]Ref<string> pointing to user.tags[0]Literals, map values, and constants have no address.
const LIMIT = 10let ages = Map.new<string, int>()
let a = &42error: cannot take address of literallet b = &ages["Bob"]error: cannot take address of map index expressionlet c = &LIMITerror: const bindings are not addressableDereferencing
Section titled “Dereferencing”.* reads the value that a reference points to:
let name = name_ref.*"Alice"let tag = tag_ref.*"admin"Mutation
Section titled “Mutation”Only a mut Ref<T> accepts writes. To get one, take & of a writable value:
fn bump(n: mut Ref<int>) { n.* += 1}
let mut count = 1let total = 1
bump(&count)&count is a mut Ref<int>bump(&total)error: expected mut Ref<int>, found Ref<int>Implicit & and .* in method calls
Section titled “Implicit & and .* in method calls”In a method call, Lisette auto-adds & or .* to the receiver as needed:
struct Rectangle { width: float64, height: float64,}
impl Rectangle { fn area(self) -> float64 { self.width * self.height }
fn scale(self: mut Ref<Rectangle>, factor: float64) { self.width *= factor }}
let mut rect = Rectangle { width: 10.0, height: 5.0 }let rect_ref = &rect
rect_ref.area()same as rect_ref.*.area()rect.scale(2.0)same as (&rect).scale(2.0)