Early preview: syntax, standard library APIs, and tooling may change.

Core DataRecords and Structs

Records and Structs

Records are the default named-field type; structs are small primitive value types.

Named Fields

Use dot access such as player.points to read a named field.

record Player { name: String, score: Int }
player.score

Record Literals

Record literals name every field with field = value.

player: Player = {name = "Ada", score = 91}

Record Update

{ old | field = value } returns a new record value.

next: Player = { player | score = player.score + 1 }

Generic Records

Records can take type parameters when the same named-field shape should carry different value types.

record Page[Item] {
	items: List[Item],
	next: Option[String]
}


names: Page[String] = {
	items = ["Ada", "Grace"],
	next = Some("page-2")
}

scores: Page[Int] = {
	items = [91, 100],
	next = None
}
-- names.items.get_or(0, "") == "Ada"
-- scores.items.get_or(1, 0) == 100
-- names.next == Some("page-2")

Structs As Value Types

Structs are stack value types for small fixed data with primitive fields. They cannot hold generic values. (Records are typically heap-allocated)

struct Point {
	x: Float,
	y: Float
}


p: Point = {
	x = 1.0,
	y = 2.0
}

Example

player.brp
record Player {name: String, points: Int}


struct Delta {points: Int}


pure func award(player: Player, event: Delta) -> Player:
	{ player | points = player.points + event.points }


func main(args: List[String]) -> Void:
	start: Player = {name = "Ada", points = 10}
	updated: Player = award(start, {points = 5})
	print(start.points) -- prints: 10
	print(updated.points) -- prints: 15

Try it

terminal
blorp run player.brp