Skip to content

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. Open it in the playground to see the boundary the constructor draws.

opaque-type.bynk
commons 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)
}