Int
int is not the only integer type; it is merely the default type for integer literals. MushScript’s integers come in the following widths:
| Family | Types | Literal suffix |
|---|---|---|
| Signed | int8 int16 int32 int64 | i8 i16 i32 i64 |
| Unsigned | uint8 uint16 uint32 uint64 | u8 u16 u32 u64 |
int is shorthand for int32, and uint for uint32. Suffixes attach directly to the literal and are case-insensitive:
val a = 42;
val big = 5_000_000i64;
val hexv = 0xFF_u8;
val bits = 0b1010;
print(a, big, hexv, bits);42 5000000 255 10Literal rules
| Form | Meaning |
|---|---|
42 | decimal |
0xFF / 0XFF | hexadecimal (prefix case-insensitive) |
0b1010 / 0B1010 | binary |
0o777 / 0O777 | octal |
5_000_000 | underscore separators (allowed in any radix) |
- Radix literals can carry suffixes too (
0xFF_u8is legal); - The single-letter suffixes
L/U/ULdo not exist; writing one produces a generic syntax error, and an illegal integer suffix reports MS3137; - A bare integer with no suffix resolves to 32-bit
int.
Literals bend to fit the target type
With a type annotation in place, a literal automatically adapts to the target type, as long as it fits:
val tiny: int8 = 127;
val wide: int64 = 9_000_000_000;
val un: uint8 = 200;
print(tiny, wide, un);127 9000000000 2009_000_000_000 is far beyond 32 bits, but the target int64 can represent it, so the compiler allows it; outside the range, the compiler refuses on the spot. The minus sign is checked together with the literal and refused the same way when out of range:
val tooBig: int8 = 200; // ✗ 200 is outside int8's range
val tooLow: int8 = -200; // ✗ -200 is outside int8's rangeWhen the target type annotation feels long, write the suffix on the number instead (the unary minus takes part in the check):
val deep = -100i8;
print(deep);-100The family also has a byte, 8-bit unsigned, same width as uint8. When you meet one, read it as uint8.
Common functions and formatting
Most of the method surface integers lack lives on the Math module (usable as bare names; the standard library is injected by default):
val n = 42;
print(n.ToString("X"));
print(Math.Abs(-7), Math.Max(2, 9), Math.Min(2, 9), Math.Sign(-3), Math.Clamp(5, 0, 3));2A
7 9 2 -1 3ToString()with no argument converts to text; with a format verb (F/N/X/P) it follows the same table as the interpolation{n:X}— see Interpolation;Math.Abs/Max/Min/Sign/Clampare five common arithmetic functions; to check overflow explicitly use theMath.CheckedAddfamily (the default is fixed-width wraparound, unchecked);- All of these functions are on the compile-time constant whitelist and may appear directly in a const position.
Notes
- Overflow and division-by-zero behavior for integer arithmetic is covered on the Operators page;
- There is no implicit conversion between integers and decimals; see Type Conversions for how to convert;
- Numeric formatting output (thousands separators, hexadecimal, and so on) is covered under Interpolation.
