Home / Docs / Guides

Fixed-width integers

Release v0.4.0

Int stays unbounded. I8, I16, I32, I64, U8, U16, U32 and U64 exist where the width matters (spec/18).

una
module demo

--- A literal takes the type it is checked against: `Some(3)` here is an `Option[U8]`.
pub default_level() -> Option[U8] = Some(3)

--- The guard on `Int` proves that `a + b` fits a `U8`.
pub add_bytes(a: U8, b: U8) -> U8 = if a.to_int() + b.to_int() <= 255 then a + b else 255

--- Wrapping arithmetic only by name.
pub hash_step(h: U32, byte: U8) -> U32 = h.wrapping_mul(by: 31).wrapping_add(by: byte.to_u32())

--- A `reflect` body computes exactly, with no overflow obligation.
pub predicate reflect large(x: U32) -> Bool = x * 4294967295 > 4294967295
una
-- fails with U5001
module demo

pub add(a: U8, b: U8) -> U8 = a + b