Skip to content

Types & Values

Pyle is dynamically typed. A variable holds a value, and the value carries its own type. There are no declarations that change runtime behavior and no implicit conversions between unrelated types.

Type Literal Notes
Int 42, 0xFF 64-bit signed, wraps on overflow
Float 3.14 Double precision
Bool true, false
None none Absence of a value
String "hello" Interned, immutable byte string
Array [1, 2, 3] Heterogeneous, resizable
Map {a: 1, "b": 2} Keys can be strings, ints, and other hashable values
Range 1..10 Exclusive end
Tuple (1, 2) Fixed-size immutable sequence, 2+ elements
Function fn(x) x + 1 First-class closure
StructType struct Point(x, y) {} Type definition
Struct Point(1, 2) Fixed-shape instance
Bytes Bytes([65, 66]) Immutable byte buffer
BytesBuilder BytesBuilder() Mutable byte writer
Coro Coro(my_fn) Coroutine
Future Future() Async result handle
Iterator produced by iteration Internal iteration state
NativeObject produced by bindings Wrapped C++ object

typeof returns a string describing a value.

print(typeof(42)) # int
print(typeof(3.14)) # float
print(typeof("hello")) # string
print(typeof([1, 2])) # array
print(typeof({a: 1})) # map
print(typeof(fn() => 1)) # function
print(typeof(none)) # none

For a struct type, typeof(P) returns a heap reference name such as "HeapRef(12)"; tostring(P) returns "struct {fields}". For a struct instance, typeof(instance) returns "struct" and tostring(instance) returns {field: value, ...}.

Strings are immutable, interned byte strings. The same string content shares one heap allocation.

let s = "hello"
print(s.size()) # 5
print(s[0]) # h
print(s.slice(1, 3)) # el
print(s.upper()) # HELLO
print(s.split("l")) # ["he", "", "o"]

String indexing is checked at runtime. String comparison with <, <=, >, or >= raises TypeError; only == and != compare strings.

Arrays are reference types with indexed access and mutation.

let values = [1, 2, 3]
values.append(4)
values[0] = 10
print(values) # [10, 2, 3, 4]
print(values.slice(1, 3)) # [2, 3]

There are no negative indices. Use values[values.size() - 1] for the last element. Out-of-bounds access raises IndexError.

Maps store key-value pairs. Keys can be strings, ints, floats, bools, and other hashable values. Arrays, maps, struct instances, and BytesBuilder cannot be keys.

let person = {name: "Ada", age: 36}
print(person.name) # Ada
print(person["age"]) # 36
person["age"] = 37
person.city = "London" # dot assignment writes the data key
print(person.has("city")) # true
print(person.keys())
print(person.values())

A missing key through brackets raises IndexError. Dot access returns none for a missing key.

Ranges are created with .. and have an exclusive end.

for i in 1..5 {
print(i) # 1, 2, 3, 4
}
let empty = 5..1

Range boundaries must be integers. A float boundary raises TypeError.

A tuple is a fixed-size, ordered, immutable sequence of values, written as zero or more comma-separated values inside parentheses. () is the empty tuple. A single parenthesized value without a comma stays a grouping, so (1 + 2) is 3 and (1) is the int 1; a one-element tuple needs the trailing comma, (1,).

let point = (3, 4)
let mixed = (1, "two", 3.5)
print(point) # (3, 4)
print(point[0]) # 3
print(point.size()) # 2
print(typeof(point)) # tuple
print((1 + 2) * 3) # 9, grouping

() is the empty tuple, (1) is the int 1, and (1,) is a one-element tuple. Indexing is 0-based like arrays, and out-of-bounds access raises IndexError. Tuples are immutable and expose only .size() and .to_array(), so there is no append, insert, or sort.

An array converts to a tuple with .to_tuple() and back with .to_array(). Both copy: the result is independent of the source, so mutating one does not affect the other. An empty array converts to the empty tuple.

The usual purpose is returning several values from a function as one value; index the result instead of writing a struct for a throwaway pair.

fn min_max(arr) {
let lo = arr[0]
let hi = arr[0]
for x in arr {
if x < lo { lo = x }
if x > hi { hi = x }
}
return (lo, hi)
}
let r = min_max([5, 2, 9, 1, 7])
print(r) # (1, 9)
print(r[0]) # 1
print(r[1]) # 9

Tuples compare by identity, so (1, 2) == (1, 2) is false and a tuple cannot be used as a map key. C++ embedding maps them to and from std::tuple and std::pair.

Functions are closures and first-class values.

fn add(a, b) => a + b
let f = add
print(f(2, 3))

A function value can be stored in an array or map, passed as an argument, or returned from another function.

Structs are fixed-shape reference types with fields and methods.

struct Point(x, y) {
fn length() => self.x + self.y
}
let p = Point(3, 4)
print(p.length())

Fields must be declared in the struct header. You cannot add a new field after construction.

== and != compare values:

  • Ints and floats compare numerically across types: 1 == 1.0 is true.
  • true == 1 is false.
  • Strings compare by content.
  • Arrays, maps, structs, bytes, functions, ranges, coroutines, tuples, and native objects compare by identity. [] == [] is false.
print(1 == 1.0) # true
print("a" == "a") # true
print([] == []) # false
let a = [1]
let b = a
print(a == b) # true, same object

int, float, bool, string, array, map, and bytes are string constants. They are useful with typeof.

let value = 42
if typeof(value) == int {
print("integer")
}

There are no int(), float(), bool(), or string() conversion functions. Use toint, tofloat, tonum, tostring, and the string parsing methods.

int, float, bool, and none are value types with no methods. Use the global conversion functions:

Function Returns Description
toint(v: any) int | none Truncate toward zero
tofloat(v: any) float | none Widen or parse
tonum(v: any) int | float | none Parse a number
tostring(v: any) string Display string
typeof(v: any) string Type name

See Built-in Functions for details.