Skip to content

22 · Temporal Expressions

Declared unit suffixes

Attach @unit to a scalar newtype to give literals a typed suffix. The suffix preserves the declared nominal owner and scalar precision. A suffix alone does not define a conversion scale: declare dimensions and scales when automatic conversion is needed.

On this page

Attach @unit to a scalar newtype to give literals a typed suffix. The suffix preserves the declared nominal owner and scalar precision. A suffix alone does not define a conversion scale: declare dimensions and scales when automatic conversion is needed.

Complete example

Declared unit suffixes.

20-typed-units.zolo
Playground
// Declared suffixes retain exact scalar spelling and nominal ownership.

@unit("kg") newtype Mass(decimal) where |value| value >= 0d
@unit("g") newtype Grams(decimal)
@unit("crate") newtype Crates(int)

let parcel = 2.5kg
let total = parcel + 0.5kg
print(total.unwrap()) // expected: 3.0

print((parcel * 2d).unwrap()) // expected: 5.0

print(parcel / 0.5kg) // expected: 5

print(parcel > 1kg) // expected: true


let grams = 500g
let converted = Mass.new(grams.unwrap() / 1000d).unwrap()
print(converted.unwrap()) // expected: 0.50


let stock = 8crate
print((stock / 2).unwrap()) // expected: 4

print(stock / 2crate) // expected: 4

Rules and behavior

Validation, schemas and units · Documentation

From a checkout of the repository:

zolo examples/features/01-syntax-and-types/20-typed-units.zolo

Global index

Find your way through Zolo

Try an idea

Start here

9 results

9 results

enespt-br