types
The types module provides runtime type inspection and value conversion. Top-level helpers check a value’s type, while submodules group parsing, formatting, and predicates for each primitive type. Import it with import types.
Top-level
| Function | Signature | Description |
|---|
isString | types.isString(v: any) → bool | Whether v is a string |
isInt | types.isInt(v: any) → bool | Whether v is an int |
isFloat | types.isFloat(v: any) → bool | Whether v is a float |
isBool | types.isBool(v: any) → bool | Whether v is a bool |
isArray | types.isArray(v: any) → bool | Whether v is an array |
isVoid | types.isVoid(v: any) → bool | Whether v is void/null |
typeOf | types.typeOf(v: any) → string | Name of the value’s type |
types.int
| Function | Signature | Description |
|---|
parse | types.int.parse(s: string) → int | Parse a decimal int, 0 on failure |
toBin | types.int.toBin(n: int) → string | Binary form with 0b prefix |
toHex | types.int.toHex(n: int) → string | Hex form with 0x prefix |
toOct | types.int.toOct(n: int) → string | Octal form with 0o prefix |
fromBin | types.int.fromBin(s: string) → int | Parse binary (optional 0b), 0 on failure |
fromHex | types.int.fromHex(s: string) → int | Parse hex (optional 0x), 0 on failure |
fromOct | types.int.fromOct(s: string) → int | Parse octal (optional 0o), 0 on failure |
max | types.int.max() → int | Largest representable int |
min | types.int.min() → int | Smallest representable int |
isInt | types.int.isInt(v: any) → bool | Whether v is an int |
types.float
| Function | Signature | Description |
|---|
parse | types.float.parse(s: string) → float | Parse a float, 0.0 on failure |
round | types.float.round(f: float, decimals: int) → float | Round to decimals places |
toFixed | types.float.toFixed(f: float, decimals: int) → string | Format with a fixed number of decimals |
isNaN | types.float.isNaN(v: any) → bool | Whether v is NaN |
isInf | types.float.isInf(v: any) → bool | Whether v is infinite |
isFloat | types.float.isFloat(v: any) → bool | Whether v is a float |
types.bool
| Function | Signature | Description |
|---|
parse | types.bool.parse(s: string) → bool | Parse true/1/yes/on as true |
toString | types.bool.toString(b: bool) → string | "true" or "false" |
isBool | types.bool.isBool(v: any) → bool | Whether v is a bool |
types.char
Each function takes a one-character string.
| Function | Signature | Description |
|---|
code | types.char.code(c: string) → int | Byte code of the character |
fromCode | types.char.fromCode(code: int) → string | Character for the codepoint |
isUpper | types.char.isUpper(c: string) → bool | Whether the char is uppercase |
isLower | types.char.isLower(c: string) → bool | Whether the char is lowercase |
isDigit | types.char.isDigit(c: string) → bool | Whether the char is a digit |
isAlpha | types.char.isAlpha(c: string) → bool | Whether the char is a letter |
isSpace | types.char.isSpace(c: string) → bool | Whether the char is whitespace |
toLower | types.char.toLower(c: string) → string | Lowercased char |
toUpper | types.char.toUpper(c: string) → string | Uppercased char |
types.string
| Function | Signature | Description |
|---|
is | types.string.is(v: any) → bool | Whether v is a string |
empty | types.string.empty(v: any) → bool | Whether v is an empty string |
nonEmpty | types.string.nonEmpty(v: any) → bool | Whether v is a non-empty string |
default | types.string.default(v: any, default: string) → any | v unless it is void or empty, then default |
coerce | types.string.coerce(v: any) → string | String form of any value |
length | types.string.length(v: any) → int | Byte length, 0 if not a string |
types.null and types.void
| Function | Signature | Description |
|---|
is | types.null.is(v: any) → bool | Whether v is null/void |
value | types.null.value | The null/void value |
is | types.void.is(v: any) → bool | Whether v is void |
value | types.void.value | The void value |
Example
import types
let n = types.int.parse("255")
print(types.int.toHex(n)) // 0xff
let v = "hello"
print(types.typeOf(v)) // string
print(types.string.default("", "fallback")) // fallback