Skip to content

References

Ref<T> is a reference to a value of type T, equivalent to a Go pointer but guaranteed non-nil.

Use & to take a reference to anything that has an address, such as a variable, a field, or an element. Literals, map values, and const bindings have none.

let user = User { name: "Alice", tags: ["admin"] }
let user_ref = &userRef<User> pointing to user
let name_ref = &user.nameRef<string> pointing to user.name
let tag_ref = &user.tags[0]Ref<string> pointing to user.tags[0]

Use .* to follow a reference back to the pointee:

let name = name_ref.*"Alice"
let tag = tag_ref.*"admin"

Only a mut Ref<T> can be written to:

fn bump(n: mut Ref<int>) {
n.* += 1
}
let mut count = 1
bump(&count)&count is a mut Ref<int>
let total = 1
bump(&total)error: expected mut Ref<int>, found Ref<int>

Refs only grant what the place they point to allows. &x is a mut Ref<T> if x allows writes, else a plain Ref<T>.

When calling methods, Lisette auto-adds & or .* 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 shape = &rect
let a = shape.area()equivalent to shape.*.area()
rect.scale(2.0)equivalent to (&rect).scale(2.0)