Skip to content

Comptime int

Chung Leong edited this page Jun 2, 2024 · 6 revisions

Comptime int is the type for integer literals in the Zig language.

pub const width = 4;

In the above statement, 4 is comptime_int. Because width doesn't have a type specifier, it gets the type from right side. Hence it too is comptime_int.

Comptime integers are represented as either number or bigint depending on whether their values can fit within a 32-bit integer. This arrangement can be problematic in situations where constants from Zig interact with each others, since JavaScript does not allow the mixing of number and bigint.

pub const base = 0x0000_FFFF_FFFF_FFFF;
pub const offset = 16;
import { base, offset } from './comptime-int-example-1.zig';

try {
    const address = base + offset;
} catch (err) {
    console.log(err.message);
}
Cannot mix BigInt and other types, use explicit conversions

The solution is to provide a specific type in such situations:

pub const base: usize = 0x0000_FFFF_FFFF_FFFF;
pub const offset: usize = 16;

Comptime float

Clone this wiki locally