Integers
Int is the default whole-number type; use sized types when the bit width is part of the contract.
`Int`
Int is a 64-bit signed integer and is the default type for whole-number literals.
count: Int = 42
-- count == 42
Sized Integers
Use Int8, UInt8, Int32, and related types when storage size or binary layout matters.
func main(args: List[String]) -> Void:
byte: UInt8 = to_uint8(255)
small: Int32 = to_int32(42)
-- byte == 255
-- small == 42
Wrapping Defaults
Integer overflow wraps, and integer division or modulo by zero returns 0 by default.
func main(args: List[String]) -> Void:
wrapped: UInt8 = to_uint8(255) + to_uint8(1)
zero_on_divide_by_zero: Int = 10 / 0
-- wrapped == 0
-- zero_on_divide_by_zero == 0
Bounds
INT_MIN and INT_MAX name the signed 64-bit range.
import:
int: INT_MAX, INT_MIN
range: (Int, Int) = (INT_MIN, INT_MAX)
-- range[0] == -9223372036854775808
-- range[1] == 9223372036854775807
Checked Helpers
Use helpers such as divide_checked, add_checked, or mod_checked when callers need an explicit Result.
import:
int: divide_checked
match divide_checked(10, 0):
Ok(n): print(n)
Err(_): print("cannot divide") -- prints: cannot divide
Example
integers.brp
import:
int: add_checked, divide_checked
func main(args: List[String]) -> Void:
byte: UInt8 = to_uint8(255)
wrapped: UInt8 = byte + to_uint8(1)
match divide_checked(10, 0):
Ok(value): print(value)
Err(_): print("cannot divide") -- prints: cannot divide
match add_checked(40, 2):
Ok(value): print(value) -- prints: 42
Err(_): print("overflow")
print(wrapped) -- prints: 0
Try it
terminal
blorp run examples/docs/core-data/integers/integers.brp