Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Types

This chapter describes the type system of the Forge programming language.

Overview

Forge is dynamically typed at runtime: variables do not have fixed types, and any variable may hold a value of any type at any point during execution. However, Forge supports optional type annotations (gradual typing) on variable declarations, function parameters, and return types. These annotations serve as documentation and enable the optional type checker to detect certain errors before execution.

Every value in Forge belongs to exactly one of the following type categories:

CategoryTypes
PrimitiveInt, Float, String, Bool, Null
CollectionArray, Object
StructUser-defined via struct / thing
InterfaceUser-defined via interface / power
FunctionNamed functions, closures, lambdas
Algebraic (ADT)User-defined via type Name = Variant | ...
ResultOk(value), Err(message)
OptionSome(value), None

Type Annotations

Type annotations use a colon after the name, followed by the type:

let name: String = "Alice"
let age: Int = 30
let score: Float = 98.5
let active: Bool = true

Function parameters and return types may also be annotated:

fn add(a: Int, b: Int) -> Int {
    return a + b
}

When annotations are omitted, types are inferred from the assigned values. Annotations are always optional.

Type Inspection at Runtime

The built-in typeof() function (aliased as type()) returns a string describing the runtime type of a value:

say typeof(42)                 // Int
say typeof(3.14)               // Float
say typeof("hello")            // String
say typeof(true)               // Bool
say typeof(null)               // Null
say typeof([1, 2, 3])          // Array
say typeof({ name: "Alice" })  // Object

For struct instances, typeof() returns the struct name (e.g., "Person").

Truthiness

When a value is used in a boolean context (such as an if condition), Forge evaluates it as “truthy” or “falsy” according to the following rules:

ValueTruthy?
falseFalsy
nullFalsy
0 (integer zero)Falsy
0.0 (float zero)Falsy
"" (empty string)Falsy
[] (empty array)Falsy
Everything elseTruthy

Subsections

The following subsections define each type category in detail: