Pre-release Harn is pre-1.0 — the language, standard library, and CLI may change between releases. See the release notes

Structs#

Structs define named record types with typed fields. Structs may also be generic.

Struct declaration#

struct Point {
  x: int
  y: int
}

struct User {
  name: string
  age: int
}

struct Pair<A, B> {
  first: A
  second: B
}

Fields are declared with name: type syntax, one per line.

Struct construction#

Struct instances can be constructed with the struct name followed by a named-field body:

struct Point { x: int, y: int }
struct User { name: string, age: int }
struct Pair<A, B> { first: A, second: B }
const p = Point { x: 3, y: 4 }
const u = User { name: "Alice", age: 30 }
const pair: Pair<int, string> = Pair { first: 1, second: "two" }

Field access#

Struct fields are accessed with dot syntax, the same as dict property access:

harness.obs.log(p.x)    // 3
harness.obs.log(u.name) // "Alice"