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

Program StructureTraits

Traits

Traits name behavior that generic code can require.

Standard Traits

Standard traits include Stringable, Equatable, Orderable, and numeric operator traits.

func main(args: List[String]) -> Void:
	text: String = 42.to_string()

Bounds

A bound such as T: Orderable lets a generic function use ordering operations.

pure func larger[T:Orderable](a: T, b: T) -> T:
	if a > b:
		a
	else:
		b

Operator Traits

Operators are available when the type implements the required trait.

pure func add_pair[T:Addable](a: T, b: T) -> T:
	a + b

Implementing Traits

An implements block provides trait functions for a type.

implements Stringable for User:
	pure func to_string(user: User) -> String:
		user.name

Explicit Equality

Records and payload unions need an Equatable implementation for == and !=. There is no identity fallback; this example is rejected.

record Point {x: Int, y: Int}


pure func same_point(a: Point, b: Point) -> Bool:
	a == b

Records as Dictionary Keys

Custom keys implement Equatable and Hashable over the same fields. Equal keys must have equal hashes. These separately constructed points retrieve the same entry.

import:
	dict: dict, get_or, set


record Point {x: Int, y: Int}


implements Equatable for Point:
	pure func equals(a: Point, b: Point) -> Bool:
		a.x == b.x and a.y == b.y


implements Hashable for Point:
	pure func hash(point: Point) -> Int:
		hash_combine(point.x.hash(), point.y.hash())


func main(args: List[String]) -> Void:
	a: Point = {x = 2, y = 3}
	b: Point = {x = 2, y = 3}
	labels: Dict[Point, String] = dict().set(a, "start")
	print(a == b) -- prints: True
	print(labels.get_or(b, "missing")) -- prints: start

Example

users.brp
record User {name: String, score: Int}


implements Stringable for User:
	pure func to_string(user: User) -> String:
		user.name + ": " + user.score.to_string()


func main(args: List[String]) -> Void:
	users: List[User] = [{name = "Ada", score = 91}]
	match users.get(0):
		Some(user): print(user.to_string()) -- prints: Ada: 91
		None: print("no users")

Try it

terminal
blorp run users.brp