Early preview: syntax, standard library APIs, and tooling may change.

Numerics and SystemsNumbers and Safe Arithmetic

Numbers and Safe Arithmetic

Blorp keeps numeric behavior explicit: default arithmetic is infallible, and checked helpers model failure.

Core Numeric Types

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)
-- wrapped == 0

Checked Helpers

Use helpers such as divide_checked, add_checked, or mod_checked when a failure value matters.

import:
    int: divide_checked

match divide_checked(10, 0):
    Ok(n): print(n)
    Err(_): print("cannot divide") -- prints: cannot divide

Example

numbers.brp
import:
	int: divide_checked


func main(args: List[String]) -> Void:
	byte: UInt8 = to_uint8(255)
	flags: UInt8 = bit_or(to_uint8(1), shift_left(to_uint8(1), 3))

	match divide_checked(10, 0):
		Ok(value): print(value)
		Err(_): print("cannot divide") -- prints: cannot divide

	print(byte) -- prints: 255
	print(flags) -- prints: 9

Try it

terminal
blorp run numbers.brp