Home / Docs / Standard library / Built-in methods
to_u16
A built-in method, resolved by the type of its receiver (spec/14 §2). Edition 0.2 and later.
x.to_u16() -> U16
where x: Int
x.to_u16() -> U16
where x: F, one of I8, I16, I32, I64, U8, U16, U32, U64Converts x to that type. A widening conversion, where every value of the source type fits the target, always succeeds; a narrowing one carries an overflow obligation that the value fits, or goes through checked, which counts against the escape budget (spec/18 §4).
Receiver
Int: an integerxF, one of I8, I16, I32, I64, U8, U16, U32, U64: a fixed-width integerx, of one of the eight typesI8,I16,I32,I64,U8,U16,U32andU64
Returns
U16: the value of x as a U16: from U8 and U16 the conversion widens, so it always succeeds; from Int and every other type it narrows, and a narrowing conversion carries an overflow obligation that the value fits, which una check proves at the call, or goes through checked, which counts against the escape budget.
See also
Example
module shop
--- A stock level kept in two bytes, where it fits.
stocked(units: Int) -> U16 = if units >= 0 and units <= 65535 then units.to_u16() else 0
pub main() -> Unit
uses @console: Console[print]
=
let lamps = stocked(1200)
let broken = stocked(-3)
@console.print("{lamps}")
@console.print("{broken}")una run . prints:
1200
0Specification
Generated from una v0.4.0 (docs/std/builtins.json, which is not part of the specification): built-in methods have no declaration, so una writes these signature lines and tests each against its checker. The specification governs: spec/14 §2.