Types and contracts
What is nominal typing?
On this page
Nominal typing recognises declared type identities and relationships. In Java two interfaces can declare the same method, yet a class implementing only one need not be assignable to the other. Distinct nominal types can prevent values such as CustomerId and InvoiceId from being accidentally interchanged.
Worked example
Ruststruct InvoiceId(u64);
struct CustomerId(u64);
fn invoice_number(id: InvoiceId) -> u64 { id.0 }
fn main() {
let customer = CustomerId(7);
assert_eq!(invoice_number(InvoiceId(7)), customer.0);
// invoice_number(customer) would fail type checking.
}Explore class relationships in the OOP Playground