Skip to content

std::structs

Struct is the interface every struct satisfies, named or anonymous. Its one function, Struct.update(original, updates), returns a copy of a struct with some fields replaced.

Distinct<T> is the interface every distinct type type X T satisfies. It has no functions; it is a bound that lets an interface default take self apart (self(n)) and build one (self(x)).

Import with import std::structs.

Interfaces

interface Struct
    fn update(original: self, updates: Partial<self>) -> self
end

The interface every struct satisfies: a named struct type and an anonymous {...} alike. No other type satisfies it. Struct also serves as a bound (fn f<T>(x: T) -> T where T: Struct) and as a parameter type that takes any struct (fn f(s: Struct)).

fn update(original: self, updates: Partial<self>) -> self

interface Struct

Returns a copy of original with the fields updates names replaced by its values. Every other field keeps its value. updates is a Partial<self>: it may omit fields, and each field it writes is a whole value, or a literal with a bare .. that updates the field’s current value ({address: {.., city: "NYC"}} changes only the city).

original = Point{x: 1, y: 2}

Struct.update(original, {x: 10}) // → Point{x: 10, y: 2}
interface Distinct<T>
end

The universal distinct-type interface. Every distinct type that wraps a value, type Celsius Decimal, satisfies Distinct<T> for exactly the type it wraps, so Celsius is a Distinct<Decimal>; a zero-sized type Done wraps nothing and satisfies no Distinct<T>. It is never implemented by hand.

It has no functions. Its use is the bound where self: Distinct<T> on an interface default, which lets the default take the implementing type apart with the pattern self(p) and build one with self(x), so a family of distinct types shares one body:

interface Temperature
    fn unit(t: self) -> String

    fn label(self(n) as t) -> String where self: Distinct<Decimal>
        "${Decimal.round(n, 1, .HalfUp)} ${Temperature.unit(t)}"
    end

    fn scale(self(n), by: Decimal) -> self where self: Distinct<Decimal>
        self(n * by)
    end
end

T may be a type parameter of the interface, so one interface serves wrappers of different types. An opaque type implemented outside its own file gets no default that uses self(p) or self(x); it writes that function instead.