Home / Docs / Standard library / Prelude
is_json_number
A predicate of the prelude, in scope in every module. Edition 0.2 and later.
pub predicate opaque is_json_number(t: Text) -> BoolWhether t is one JSON number of RFC 8259 §6 (spec/23 §1). Opaque: both engines evaluate it.
Parameters
t: the text
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
Example
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:
price 9.95
12 is not a decimal
price 1.5e3Specification
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.