Home / Docs / Standard library / Prelude

is_json_number

Release v0.4.0Edition 0.2 and later

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

una
pub predicate opaque is_json_number(t: Text) -> Bool

Whether t is one JSON number of RFC 8259 §6 (spec/23 §1). Opaque: both engines evaluate it.

Parameters

Returns

Bool: true exactly when t is one JSON number of RFC 8259 §6 with a fraction or an exponent; opaque: both engines evaluate it, and the verifier knows nothing more of it, so in code it is called only as the predicate of checked, and elsewhere in code is U2016; a kind's where clause, as in JsonNumber, the requires and ensures of a capability operation, and the body of another opaque predicate may name it (spec/03 §4, ADR 0042); checked counts against [escape_budget] checked_per_module, which is 0 unless una.toml raises it.

See also

JsonNumber

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.