Appendix B

Built-in Types and Operations

This appendix summarizes the interpreter-level built-ins documented in the Nex Reference. It is not intended to replace the full reference pages, but to give one compact place to look up the core types used throughout the tutorial.

Global Built-in Functions

Name Signature Purpose
print print(...args) Write values to interpreter output.
println println(...args) Line-oriented output.
type_of type_of(value): String Return runtime type name.
type_is type_is(type_name: String, value): Boolean Check runtime type compatibility.
sleep sleep(ms: Integer) Pause the current task for ms milliseconds.
await_all await_all(tasks: Array[Task]) Block until all tasks in the array complete.
await_any await_any(tasks: Array[Task]) Block until at least one task in the array completes.

Foundational Protocol Types

Type Main Features Notes
Function call0 through call32 Invocation protocol for function-like values.
Comparable compare Ordering support for scalars and other comparable values.
Hashable hash Required for map keys.
Cursor start, item, next, at_end Iteration protocol used by across.

Scalar Types

String

Common operations:

  • length
  • index_of
  • substring(start, end)
  • to_upper, to_lower
  • to_integer, to_real
  • contains, starts_with, ends_with
  • trim, replace
  • char_at
  • split
  • to_bytes (an Array[Byte] of the UTF-8 encoding), and create String.from_bytes(bytes) for the reverse
  • compare, hash, cursor

Example:

let s: String := "  Nex  "
print(s.trim().to_upper())   -- "NEX"
print(s.split(" "))          -- array of pieces

Integer

Common operations:

  • to_string
  • abs, min, max
  • pick
  • to_byte, to_integer16, to_integer32 (checked conversions to the fixed-width types)
  • bitwise_left_shift, bitwise_right_shift, bitwise_logical_right_shift
  • bitwise_rotate_left, bitwise_rotate_right
  • bitwise_is_set, bitwise_set, bitwise_unset
  • bitwise_and, bitwise_or, bitwise_xor, bitwise_not
  • plus, minus, times, divided_by
  • compare, hash

Integer is a signed 64-bit integer on every backend, with checked arithmetic: overflow and division by zero raise. Bitwise operations use 32-bit integer semantics. Bit 0 is the least-significant bit. When calling a bitwise method on an integer literal, wrap the literal in parentheses:

print((5).bitwise_left_shift(1))   -- 10
print((6).bitwise_and(3))          -- 2
print((5).bitwise_is_set(0))       -- true

Real

Common operations:

  • to_string
  • abs, min, max, round, to_fixed
  • is_nan, is_infinite, is_finite
  • arithmetic operations
  • compare, hash

Real is an IEEE-754 double; division by zero yields Infinity or NaN rather than raising (integer division by zero raises).

Boolean

Common operations:

  • to_string
  • and, or, not
  • compare, hash

Char

Common operations:

  • to_string
  • to_upper, to_lower
  • compare, hash

Byte

Common operations:

  • to_string, to_hex, to_char
  • to_integer, to_integer16, to_integer32
  • min, max
  • bitwise_left_shift, bitwise_right_shift, bitwise_logical_right_shift
  • bitwise_rotate_left, bitwise_rotate_right
  • bitwise_is_set, bitwise_set, bitwise_unset
  • bitwise_and, bitwise_or, bitwise_xor, bitwise_not
  • equals, compare, hash

Byte is an unsigned 8-bit integer (0 to 255) and the element type of String.to_bytes(). Write a literal with a u8 suffix: 200u8. It does not convert to or from Integer on its own — use Integer.to_byte() (which raises unless the value is in 0..255) and Byte.to_integer(). Arithmetic on a Byte gives an Integer. The bitwise methods work on 8 bits and return a Byte, so 195u8.bitwise_not() is 60u8; bit indexes must be 0..7.

let b: Byte := 195u8
print(b.to_hex())          -- "c3"
print(b.bitwise_not())     -- 60
print(b + 1)               -- 196 (an Integer)

Integer16 and Integer32

Both types have the same operations (T stands for the type itself):

  • to_string (optionally in base 2, 8, 10 or 16)
  • to_integer, to_byte, to_integer16, to_integer32
  • abs, min, max (each returns a T)
  • bitwise_left_shift, bitwise_right_shift, bitwise_logical_right_shift
  • bitwise_rotate_left, bitwise_rotate_right
  • bitwise_is_set, bitwise_set, bitwise_unset
  • bitwise_and, bitwise_or, bitwise_xor, bitwise_not
  • equals, compare, hash

Integer16 is a signed 16-bit integer (-32768 to 32767) and Integer32 a signed 32-bit integer (-2147483648 to 2147483647). Write literals with an i16 or i32 suffix; a leading minus is part of the literal, so -5i16 is an Integer16. Neither converts implicitly to another integer type, and narrowing conversions raise an error if the value does not fit. Arithmetic gives an Integer. The bitwise methods work on, and wrap to, the type's own width: 0i16.bitwise_not() is -1i16, and 32767i16.bitwise_left_shift(1) is -2i16. Bit indexes must be 0..15 (Integer16) or 0..31 (Integer32), and abs raises for the minimum value, which has no positive counterpart.

let port: Integer16 := 8080i16
print(port.to_string(16))          -- "1f90"
print(port.bitwise_left_shift(4))  -- -1792 (129280 wrapped to 16 bits)
print((port + 1).to_integer16())   -- 8081

Collection Types

Array[T]

Construction:

[]

Main methods:

Method Purpose
get(index) Read element at index.
add(value) Append value.
add_at(index, value) Insert value.
put(index, value) Replace value at index.
length Element count.
is_empty Check emptiness.
contains(elem) Membership test.
index_of(elem) First index or -1.
remove(index) Remove element at index.
reverse Return reversed array.
sort Sort array.
slice(start, end) Subrange.
concat(other) Return a new array with other appended.
cursor Iterator for across.

Map[K, V]

Construction:

{}

Main methods:

Method Purpose
get(key) Read value for key.
try_get(key, default) Read value or fallback.
set(key, value) Add or replace an entry.
put(key, value) Alias for set.
size Number of entries.
is_empty Check emptiness.
contains_key(key) Membership by key.
keys Array of keys.
values Array of values.
remove(key) Delete entry.
cursor Iterator over entries.

Set[T]

Construction:

create Set[Integer].from_array([1, 2, 3])
#{}
#{1, 2, 3}

Notes:

  • Set literals such as #{1, 2, 3} create sets.
  • The empty set literal is #{}.
  • The empty literal {} still creates an empty map.

Main methods:

Method Purpose
contains(value) Membership test.
union(other) Set union.
difference(other) Elements in this set but not in other.
intersection(other) Common elements.
symmetric_difference(other) Elements that occur in exactly one set.
size Number of elements.
is_empty Check emptiness.
cursor Iterator for across.

Stack[T]

Stack[T] is not a built-in collection type. It is the standard tutorial example of a user-defined generic collection class built on top of Array[T].

Typical operations:

Method Purpose
push(value) Add an element to the top.
pop() Remove and return the top element.
peek() Return the top element without removing it.
is_empty() Check emptiness.
size() Number of stored elements.

Cursor Types

Concrete cursor classes:

  • ArrayCursor
  • StringCursor
  • MapCursor
  • SetCursor

They implement the Cursor protocol and are usually used indirectly through across.

System Classes

Console

Construction:

create Console

Main methods:

  • print
  • print_line
  • read_line
  • error
  • new_line
  • flush
  • read_integer
  • read_real

Process

Construction:

create Process

Main methods:

  • getenv
  • setenv
  • command_line

Task[T]

Construction:

let t: Task[Integer] := spawn do
  result := 42
end

Main methods:

  • await
  • await(ms)
  • cancel
  • is_done
  • is_cancelled

Channel[T]

Construction:

create Channel[Integer]
create Channel[Integer].with_capacity(2)

Main methods:

  • send
  • send(value, ms)
  • try_send
  • receive
  • receive(ms)
  • try_receive
  • close
  • is_closed
  • capacity
  • size

Practical Notes

  • All scalar types are modeled as Comparable and Hashable.
  • Array element count is length; map and set element counts use size.
  • Built-in method names in this appendix match the interpreter-level names.
  • Some system behavior depends on the host operating system, especially for file and process access.
  • Task and Channel are built-in concurrency abstractions available directly in Nex programs.

For the fuller per-type reference, see the Nex Reference.