Skip to content

Type System

HypnoScript relies on a static type system. Every variable, every field, and every return value has a clearly defined type that is checked at compile time. This allows many errors to be caught early and avoids runtime surprises.

Overview of Base Types

TypeDescriptionExample Code
numberDouble-precision floating-point numberinduce temperature: number = 21.5;
stringUTF-8 text, fully supports Unicodeinduce greeting: string = "Hello";
booleanTruth value true or falseinduce active: boolean = true;
tranceHypnotic state, used for sessions and suggestionsinduce state: trance = induceTrance();
arrayOrdered list with uniform element typeinduce numbers: number[] = [1, 2, 3];
recordNamed set of fields with their own typesinduce client: Client = { name, age };
objectDynamic object, typically used for external integrationsinduce data: object = loadJson();
functionFunction reference with parameters and return valueinduce analyzeUnit = suggestion(...)
sessionRunning HypnoScript sessioninduce meeting: session = beginSession();
unknownPlaceholder when the type could not yet be determinedUsed internally by the type checker

💡 Note: record, function, and array are composite types. They carry additional information (field names, parameter list, element type) that is considered during type checking.

See also Variables and Data Types for basics on declaring variables.

Type Annotation and Inference

You can specify types explicitly or let the compiler do the work:

hyp
// Explicit annotation
induce counter: number = 0;

// Type inference by the compiler
induce greeting = "Welcome"; // inferred type: string

// Explicit parameter and return types for functions
suggestion double(value: number): number {
    awaken value * 2;
}

The compiler always attempts to infer the most specific type. If it cannot make a clear statement, it internally uses unknown and reports a type warning or error.

Composite Types

Arrays

Arrays are always homogeneous. The element type is specified in square brackets after the array name:

hyp
induce names: string[] = ["Sam", "Alex", "Riley"];

induce measurements: number[];
measurements = collectValues();

For operations on arrays, the type checker ensures that only matching elements are inserted.

Records

Records combine multiple fields into a structured type:

hyp
induce Client = record {
    name: string,
    age: number,
    active: boolean
};

induce client: Client = {
    name: "Mira",
    age: 29,
    active: true
};

The structure of a record is structural – two records are compatible if they have the same field names and types.

Functions

Functions carry a complete signature type, consisting of parameter list and return value:

hyp
suggestion hypnoticGreeting(name: string, duration: number): string {
    observe name;
    observe duration;
    awaken "Welcome back";
}

Function types can be stored and passed like any other value form:

hyp
induce greetingFunction: (string, number) -> string = hypnoticGreeting;

Compatibility Rules

The type checker uses strict but pragmatic compatibility rules:

  • Primitive types must match exactly (number is not automatically compatible with string).
  • Arrays are compatible if their element types are compatible.
  • Records compare field count, field names, and field types.
  • Functions require identical parameter count and compatible parameter and return types.
  • Sessions and trance states are their own types and are not implicitly converted to other types.

If two types are not compatible, the compiler reports an error with information about the expected and the actually found type.

Working with unknown

unknown serves as a fallback when the type cannot be determined unambiguously – for example, with dynamic data sources. The goal should be to convert unknown into a concrete type as early as possible:

hyp
induce data: unknown = loadExternal();

if (isRecord(data)) {
    induce structure = cast<Client>(data);
    observe structure.name;
} else {
    warn "External data could not be interpreted.";
}

Further Resources

  • Control Flow – Type-safe decision and loop constructs
  • Functions – Signatures, return values, and inline functions
  • Records – Detailed introduction to structured data

With a clear understanding of the type system, you can write HypnoScript programs that are both hypnotic and robust.```

Released under the MIT License.