Home / Docs / Standard library / Prelude

JsonNumber

Release v0.4.0Edition 0.2 and later

A kind of the prelude, in scope in every module. Edition 0.2 and later.

una
pub kind JsonNumber = Text where is_json_number(it)

The text of a JSON number with a fraction or an exponent, which Decimal holds (spec/23 §1).

Definition

Text where is_json_number(it): the text of a JSON number with a fraction or an exponent, as Decimal holds it; since is_json_number is opaque, a JsonNumber is made by parsing, or in code by checked.

See also

is_json_number, Json

Example

src/shop.unauna
module shop

--- A price typed by hand, kept as a `JsonNumber` when it has a fraction or an exponent.
kind PriceError = | NotDecimal

priced(raw: Text) -> JsonNumber
  fails PriceError
=
  checked(raw, is_json_number(it)) else fail NotDecimal

shown(raw: Text) -> Text
=
  let outcome = attempt priced(raw)
  when outcome
    is Ok(price) -> "price {price}"
    else -> "{raw} is not a decimal"

pub main() -> Unit
  uses @console: Console[print]
=
  @console.print(shown("9.95"))
  @console.print(shown("12"))
  @console.print(shown("1.5e3"))

una run . prints:

Output
price 9.95
12 is not a decimal
price 1.5e3

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.