Home / Docs / Standard library / Prelude

to_int

Release v0.4.0Edition 0.1 and later

A function of the prelude, in scope in every module. Edition 0.1 and later.

una
pub to_int(t: Text) -> Option[Int]

Some of the integer that t writes when t is an optional - followed by one or more ASCII digits, leading zeros allowed, and None for any other text: the empty text, "-", a leading +, whitespace or digits outside ASCII. Int has no bound, so a long text of digits is never None for its length (spec/14 §1).

Parameters

Returns

Option[Int]: Some of the integer t writes, an optional - and from 1 to 4300 ASCII digits; None for any other text, among them a leading + and more than 4300 digits.

Use instead

See also

is_digits, trim

Example

src/shop.unauna
module shop

--- A stock adjustment typed by hand: a sign is allowed, so `-3` writes off three units.
adjustment(raw: Text) -> Text
=
  when to_int(raw)
    is Some(n) -> "'{raw}': adjust the stock by {n}"
    is None -> "'{raw}': not an adjustment"

pub main() -> Unit
  uses @console: Console[print]
=
  @console.print(adjustment("12"))
  @console.print(adjustment("-3"))
  @console.print(adjustment("+3"))

una run . prints:

Output
'12': adjust the stock by 12
'-3': adjust the stock by -3
'+3': not an adjustment

Specification

Generated from the standard modules of una v0.4.0 (docs/std/listing.json, which is not part of the specification). The specification governs: spec/14 §1.