Luce
Luce BaseSourceLuciaOS

9. Values

9.1 Structs#

struct Point:
    var x: float
    var y: float

    func distance(self, other: Point) -> float:
        return ((self.x - other.x) ** 2.0 + (self.y - other.y) ** 2.0) ** 0.5

    func moved(self, dx: float, dy: float) -> Point:
        return Point(x = self.x + dx, y = self.y + dy)

let origin = Point(x = 0.0, y = 0.0)
var p = origin
p.x = 5.0

A struct is a value: assignment and passing copy it, and the copy is independent. Its fields are let or var, and a var field may be assigned through a var binding. Construction is memberwise, Point(x = 0.0, y = 0.0), positional or named; a field with a default may be omitted. A struct with a custom init(self, ...) is constructed through it instead. Structs have structural == and hash when their fields do, and a display when their fields do.

9.2 Enums#

enum Shape:
    circle(radius: float)
    rectangle(width: float, height: float)
    empty

let s = Shape.circle(radius = 2.0)
let t: Shape = .empty

An enum is a closed set of cases, each with an optional named payload. Cases are constructed through the type or, where the type is known, with a leading .. Enums are values, with structural equality and hashing when their payloads have them. An enum may declare methods. There is no integer-backed enum in Luce; one that must cross to Base is declared in Base.

9.3 Tuples#

(1, "one") is a value of type (int, str); members are .0, .1, or destructured. Tuples have structural equality and ordering.

9.4 Optionals and results as values#

T? and T! are values wherever T is (§12).