Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Integers

Integers are whole numbers such as 0, 42, and -7.

Acton has three groups of integer types:

  • int for the normal 64-bit signed integer type
  • bigint for integers that must grow beyond the int range
  • explicitly sized signed and unsigned integers such as i32 and u16

bigint lets values grow arbitrarily large ensuring correct program behavior when you are uncertain about the exact size needed. However, as bigint is significantly slower than the bounded integer types, do not default to bigint out of convenience. For the vast majority of normal use cases, int is large enough and considerably faster. Use exact-width integers when you specifically need their bit width.

Bounded integer types can often be compiled in an unboxed form, which avoids boxing overhead and can make arithmetic much faster, several orders of magnitude, than bigint in tight code. That is another reason to prefer int or an exact-width integer when the bounded range is the right fit, and reserve bigint for values that truly need arbitrary precision.

If you are not sure which integer type to use, start with int. Move to bigint when values may get very large, and use the exact-width types when you need to match a protocol, file format, or external API.

Type Min Max
i8
-128 127
i16
-32768 32767
i32
-2147483648 2147483647
u1
0 1
u8
0 255
u16
0 65535
u32
0 4294967295
u64
0 18446744073709551615
int
-9223372036854775808 9223372036854775807
bigint
arbitrary arbitrary

Basic use

actor main(env):
    count = 42
    port = u16(5000)
    huge = bigint(123456789012345678901234567890)

    print("count:", count)
    print("port:", port)
    print("huge:", huge)
    print("widened:", int(port))

    env.exit(0)

Use int for everyday counting and arithmetic. Use bigint when a value may exceed the normal machine-sized range. Use exact-width types when the bit pattern matters.

Converting integers

Convert by calling the target type as a constructor.

int(42)
bigint(42)
u16(255)

Widening to a larger type is straightforward:

small = u16(255)
widened = int(small)

Converting to a narrower type checks that the value fits:

safe = u16(12345)
# u16(70000) would raise ValueError

Large integer literals are inferred by size. Values above the normal int range may infer as u64 or bigint. When you care about the exact type, annotate it or use an explicit constructor.