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

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()


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


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

Try it

terminal
blorp run users.brp