Skip to content
Playground(another site)

Opaque types

An OrderId is a String underneath, but it is its own type: a plain String is never accepted where an OrderId is required, so the two cannot be transposed by accident. This is how you stop a bare identifier — an order id, a user id, a slug — from standing in for any other string in your program.

You do not construct one directly; you go through the checked .of constructor, which returns a Result so construction can enforce whatever the id requires. Choose Edit & run in-page below (or open it in the playground) to see the boundary the constructor draws, and press Run to parse one.

opaque-type.bynk
context demo
---
An opaque type is backed by another type but is nominally distinct: an `OrderId`
is a `String` underneath, yet a plain `String` is never accepted where one is
required, so the two can't be mixed up.
---
type OrderId = opaque String
---
Construct it through the checked `.of` constructor, which returns a `Result`.
---
fn parse(raw: String) -> Result[OrderId, ValidationError] {
OrderId.of(raw)
}
---
Run calls `service playground`, which parses a raw string into an `OrderId`.
---
service playground {
on call() -> Effect[String] by Caller {
match parse("order-42") {
Ok(_) => "parsed an OrderId"
Err(e) => e.message
}
}
}