to Int() - > Option<int>- Module
- std.
bigint - Receiver
- Big
Int - Minimum arity
- 0
- Maximum arity
- 0
Declaration and parameters
LinkAvailability
Included by default: Pending
Import std.bigint in language mode 5.21.
The current compiler selects std/bigint.tpz for this language mode. Availability in other released editions and inclusion in each installed distribution are Pending.
Source evidence
- compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1115 - compiler/crates/topaz_resolve/src/stdlib.rs at
b48527d0a174· 61 - compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1115 - compiler/crates/topaz_self_frontend/topaz/checker/builtin_signatures.tpz at
b48527d0a174· 469 - compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1296 - compiler/crates/topaz_value/src/value/members.rs at
b48527d0a174· 295
Receiver
BigInt
Parameter constraints
BigInt.toInt returns Some containing the same integer when the value is at least -9223372036854775808 and at most 9223372036854775807. Both endpoints are included.
Every value outside that signed 64 bit range returns None(). Conversion does not wrap, truncate, round, or saturate.
The operation evaluates its receiver once and leaves it unchanged. A failed conversion is an Option value and does not raise an arithmetic overflow fault.
BigInt.parse can represent integers beyond the int range. BigInt.toString still formats those values exactly when BigInt.toInt returns None().
Related rule: Convert exact integers within the int rangeResult
The declared result type is Option<int>.
Effects
Failures
Result and effects
LinkBoth signed 64 bit endpoints return Some. Values outside the range return None without wrapping or saturation.
Resolved contracts
LinkRelated entries
LinkConvert exact integers within the int range
LinkConvert exact integers within the int range
BigInt.toInt returns Some containing the same integer when the value is at least -9223372036854775808 and at most 9223372036854775807. Both endpoints are included.
Every value outside that signed 64 bit range returns None(). Conversion does not wrap, truncate, round, or saturate.
The operation evaluates its receiver once and leaves it unchanged. A failed conversion is an Option value and does not raise an arithmetic overflow fault.
BigInt.parse can represent integers beyond the int range. BigInt.toString still formats those values exactly when BigInt.toInt returns None().
Why
A checked conversion preserves the distinction between an exact arbitrary precision value and a bounded machine integer. Option makes failure explicit without inventing a replacement integer.
Source evidence
- compiler/crates/topaz_value/src/value/bigint.rs at
912a5e86e1f9· 144 - compiler/crates/topaz_value/src/value/builtins/bigint_decimal.rs at
912a5e86e1f9· 66 -
topaz.stdlib.contract.bigint-to-intatworking-tree
Include both int endpoints and reject their neighbors
Linkimport std.bigint { BigInt }import std.testimport std.iofunction show(text: string) -> () {let value = test.assertSome(BigInt.parse(text, 10))match (value.toInt()) {case Some(integer) => io.writeLine("{integer}")case None() => io.writeLine("outside int")}}show("-9223372036854775808")show("9223372036854775807")show("-9223372036854775809")show("9223372036854775808")show("0")
-9223372036854775808 9223372036854775807 outside int outside int 0
Expected result
Expected result
Execution completes. Exit code 0.
-92233720368547758089223372036854775807outside intoutside int0
Source evidence
- compiler/crates/topaz_value/src/value/bigint.rs at
912a5e86e1f9· 144 - compiler/crates/topaz_value/src/value/builtins/bigint_decimal.rs at
912a5e86e1f9· 66 -
topaz.stdlib.contract.bigint-to-intatworking-tree
Keep a large value after conversion fails
Linkimport std.bigint { BigInt }import std.testimport std.iolet value = test.assertSome(BigInt.parse("1000000000000000000000000000000", 10))match (value.toInt()) {case None() => io.writeLine("outside int")case Some(_) => io.writeLine("converted")}io.writeLine(value.toString(10))
outside int 1000000000000000000000000000000
Expected result
Expected result
Execution completes. Exit code 0.
outside int1000000000000000000000000000000
Source evidence
- compiler/crates/topaz_value/src/value/bigint.rs at
912a5e86e1f9· 144 - compiler/crates/topaz_value/src/value/builtins/bigint_decimal.rs at
912a5e86e1f9· 66 -
topaz.stdlib.contract.bigint-to-intatworking-tree