Silk

silk/i32

Thirty-two-bit signed integers and the default type of context-free integer literals.

When to use

Use i32 for ordinary signed whole-number calculations whose range is known to fit, and when a stable 32-bit representation matters. Choose isize for pointer-sized offsets and i64 for a wider fixed range.

Details

Ordinary arithmetic, narrowing conversions, division by zero, and invalid shift counts trap. checked* reports invalid arithmetic with Option; wrapping* computes modulo 2^32; saturating* clamps at MIN or MAX. Signed right shifts preserve the sign bit.

Decimal parse rejects trailing bytes and separates invalid syntax from out-of-range input. toText allocates an owned decimal string.

Gotchas

MIN has no positive i32 counterpart. Use wrappingNegate or saturatingNegate for its negation boundary. Use checkedDivide when division by -1 can receive MIN.

Examples

Select an overflow result explicitly

import silk.i32 as i32

import silk.option as Option

pub fn main() -> i32 {
  let checked = i32.checkedAdd(i32.MAX, 1)
  let recovered = move checked
    |> Option.unwrapOr<i32>(42)
  if i32.wrappingAdd(i32.MAX, 1) != i32.MIN {
    return 1
  }
  if i32.saturatingAdd(i32.MAX, 1) != i32.MAX {
    return 2
  }
  return recovered
}

Import as i32 with import silk.i32.

Public declarations: 60.

MAX

pub const MAX: i32

The largest i32 value.

MIN

pub const MIN: i32

The smallest i32 value.

BITS

pub const BITS: u32

The fixed width of i32, in bits.

negate

pub fn negate(value: i32) -> i32

Returns the arithmetic negation of value and traps when value is MIN. Use this function when that boundary is a program error.

wrappingNegate

pub fn wrappingNegate(value: i32) -> i32

Returns the arithmetic negation of value, wrapped to the i32 range. MIN stays MIN. Use this function for deliberate modulo arithmetic.

saturatingNegate

pub fn saturatingNegate(value: i32) -> i32

Returns the arithmetic negation of value, clamped to the i32 range. MIN becomes MAX. Use this function when the positive boundary is required.

toU8

pub fn toU8(value: i32) -> u8

Converts value to u8. Traps if value is outside the u8 range. Use this function when an out-of-range value is a program error.

checkedToU8

pub fn checkedToU8(value: i32) -> Option<u8>

Converts value to u8, or returns None if value is outside the u8 range. Use this function when an out-of-range value is input data.

toU16

pub fn toU16(value: i32) -> u16

Converts value to u16. Traps if value is outside the u16 range. Use this function when an out-of-range value is a program error.

checkedToU16

pub fn checkedToU16(value: i32) -> Option<u16>

Converts value to u16, or returns None if value is outside the u16 range. Use this function when an out-of-range value is input data.

toU32

pub fn toU32(value: i32) -> u32

Converts value to u32. Traps if value is outside the u32 range. Use this function when an out-of-range value is a program error.

checkedToU32

pub fn checkedToU32(value: i32) -> Option<u32>

Converts value to u32, or returns None if value is outside the u32 range. Use this function when an out-of-range value is input data.

toU64

pub fn toU64(value: i32) -> u64

Converts value to u64. Traps if value is outside the u64 range. Use this function when an out-of-range value is a program error.

checkedToU64

pub fn checkedToU64(value: i32) -> Option<u64>

Converts value to u64, or returns None if value is outside the u64 range. Use this function when an out-of-range value is input data.

toUsize

pub fn toUsize(value: i32) -> usize

Converts value to usize. Traps if value is outside the usize range. Use this function when an out-of-range value is a program error.

checkedToUsize

pub fn checkedToUsize(value: i32) -> Option<usize>

Converts value to usize, or returns None if value is outside the usize range. Use this function when an out-of-range value is input data.

toI8

pub fn toI8(value: i32) -> i8

Converts value to i8. Traps if value is outside the i8 range. Use this function when an out-of-range value is a program error.

checkedToI8

pub fn checkedToI8(value: i32) -> Option<i8>

Converts value to i8, or returns None if value is outside the i8 range. Use this function when an out-of-range value is input data.

toI16

pub fn toI16(value: i32) -> i16

Converts value to i16. Traps if value is outside the i16 range. Use this function when an out-of-range value is a program error.

checkedToI16

pub fn checkedToI16(value: i32) -> Option<i16>

Converts value to i16, or returns None if value is outside the i16 range. Use this function when an out-of-range value is input data.

toI32

pub fn toI32(value: i32) -> i32

Returns value unchanged as i32. Use this function when generic conversion code can select i32 as both source and destination.

checkedToI32

pub fn checkedToI32(value: i32) -> Option<i32>

Returns Some with value unchanged as i32. Use this function when generic checked-conversion code can select the same source and destination type.

toI64

pub fn toI64(value: i32) -> i64

Converts value exactly to i64. Every i32 value is representable.

checkedToI64

pub fn checkedToI64(value: i32) -> Option<i64>

Converts value exactly to i64 and returns Some. Every i32 value is representable.

toIsize

pub fn toIsize(value: i32) -> isize

Converts value exactly to isize. Every i32 value is representable.

checkedToIsize

pub fn checkedToIsize(value: i32) -> Option<isize>

Converts value exactly to isize and returns Some. Every i32 value is representable.

toF32

pub fn toF32(value: i32) -> f32

Converts value to the nearest f32 value, with ties to even.

toF64

pub fn toF64(value: i32) -> f64

Converts value to the nearest f64 value, with ties to even.

add

pub fn add(left: i32, right: i32) -> i32

Returns left + right and traps if the result is outside the i32 range. Use this function when overflow is a program error.

subtract

pub fn subtract(left: i32, right: i32) -> i32

Returns left - right and traps if the result is outside the i32 range. Use this function when overflow is a program error.

multiply

pub fn multiply(left: i32, right: i32) -> i32

Returns left * right and traps if the result is outside the i32 range. Use this function when overflow is a program error.

divide

pub fn divide(left: i32, right: i32) -> i32

Returns left / right, rounded toward zero. Traps if right is zero or MIN is divided by -1. Use this function when an invalid quotient is a program error.

remainder

pub fn remainder(left: i32, right: i32) -> i32

Returns the remainder with the sign of left. Traps if right is zero or MIN is divided by -1. Use this function when invalid division is a program error.

bitAnd

pub fn bitAnd(left: i32, right: i32) -> i32

Returns the bitwise AND of left and right.

bitOr

pub fn bitOr(left: i32, right: i32) -> i32

Returns the bitwise OR of left and right.

bitXor

pub fn bitXor(left: i32, right: i32) -> i32

Returns the bitwise exclusive OR of left and right.

bitNot

pub fn bitNot(value: i32) -> i32

Returns value with each bit inverted.

shiftLeft

pub fn shiftLeft(left: i32, right: i32) -> i32

Shifts left bits left by right positions. Traps if right is negative or not less than BITS.

shiftRight

pub fn shiftRight(left: i32, right: i32) -> i32

Shifts left bits right by right positions and preserves its sign. Traps if right is negative or not less than BITS.

rotateLeft

pub fn rotateLeft(left: i32, right: i32) -> i32

Rotates the bits of left left by right positions.

rotateRight

pub fn rotateRight(left: i32, right: i32) -> i32

Rotates the bits of left right by right positions.

wrappingAdd

pub fn wrappingAdd(left: i32, right: i32) -> i32

Returns left + right, wrapped to the i32 range. Use this function for deliberate modulo arithmetic.

wrappingSubtract

pub fn wrappingSubtract(left: i32, right: i32) -> i32

Returns left - right, wrapped to the i32 range. Use this function for deliberate modulo arithmetic.

wrappingMultiply

pub fn wrappingMultiply(left: i32, right: i32) -> i32

Returns left * right, wrapped to the i32 range. Use this function for deliberate modulo arithmetic.

saturatingAdd

pub fn saturatingAdd(left: i32, right: i32) -> i32

Returns left + right, clamped to MIN or MAX. Use this function when a boundary value is the required overflow result.

saturatingSubtract

pub fn saturatingSubtract(left: i32, right: i32) -> i32

Returns left - right, clamped to MIN or MAX. Use this function when a boundary value is the required overflow result.

saturatingMultiply

pub fn saturatingMultiply(left: i32, right: i32) -> i32

Returns left * right, clamped to MIN or MAX. Use this function when a boundary value is the required overflow result.

checkedAdd

pub fn checkedAdd(left: i32, right: i32) -> Option<i32>

Returns Some with left + right, or None if the result is outside the i32 range. Use this function when overflow is input data.

checkedSubtract

pub fn checkedSubtract(left: i32, right: i32) -> Option<i32>

Returns Some with left - right, or None if the result is outside the i32 range. Use this function when overflow is input data.

checkedMultiply

pub fn checkedMultiply(left: i32, right: i32) -> Option<i32>

Returns Some with left * right, or None if the result is outside the i32 range. Use this function when overflow is input data.

checkedDivide

pub fn checkedDivide(left: i32, right: i32) -> Option<i32>

Returns Some with left / right, or None if right is zero or MIN is divided by -1. Use this function when an invalid quotient is input data.

checkedRemainder

pub fn checkedRemainder(left: i32, right: i32) -> Option<i32>

Returns Some with the remainder, or None if right is zero or MIN is divided by -1. Use this function when invalid division is input data.

equals

pub fn equals(left: i32, right: i32) -> bool

Returns true when left and right are equal.

notEquals

pub fn notEquals(left: i32, right: i32) -> bool

Returns true when left and right are not equal.

lessThan

pub fn lessThan(left: i32, right: i32) -> bool

Returns true when left is less than right.

lessOrEqual

pub fn lessOrEqual(left: i32, right: i32) -> bool

Returns true when left is less than or equal to right.

greaterThan

pub fn greaterThan(left: i32, right: i32) -> bool

Returns true when left is greater than right.

greaterOrEqual

pub fn greaterOrEqual(left: i32, right: i32) -> bool

Returns true when left is greater than or equal to right.

toText

pub effect fn toText(value: i32) -> String ! OutOfMemoryError ? &mut Allocator

Renders the value as base-10 text in new owned storage. Allocation uses the required Allocator and can fail with OutOfMemoryError.

parse

pub fn parse(text: string) -> silk/result.Result<i32, silk/format.ParseError>

Reads the complete text as a signed decimal i32.

Details

A failure contains silk.format.NotANumber for empty text, a leading +, a non-digit, or trailing bytes. It contains silk.format.OutOfRange if the number is outside the i32 range.

On this page