Modifiers

Modifiers

A declaration can carry modifiers in a bracketed list before it. They are all part of one list, so several can appear together:

[public] fn stringify(value: JsonValue) => string { }

[public state] count: number

Visibility

Modifier Meaning
[public] Records that the item is public API.
[private] Restricts the item to its declaring scope.

Public is the default. An item with no visibility modifier is already reachable, so [public] does not unlock anything; it records that the exposure is deliberate.

What it buys you is the unused-code warning. Something never called inside its own module looks dead, and the analyzer says so. For a library that is the normal case, because the callers are consumers the compiler cannot see. [public] marks those as intentional and settles the warning.

[private] is the modifier that changes behavior. [public] wins if both appear, since the last one applies.

Mutation and state

Modifier Applies to Meaning
[mutates] methods The method assigns to an attribute of this. Inferred; written only for clarity.
[state] fields Changes to the field notify the object. See the state page.

Other modifiers

Modifier Applies to Meaning
[static] methods No receiver; called on the type rather than an instance.
[serial] classes Derives serialization and deserialization.
[constant] declarations The value is fixed.
[external] functions The body is resolved at link time rather than declared here.
[hide] declarations Kept out of generated documentation and completion.
[gcsafe] functions Marks a function safe to run with respect to collection.
[notrack] functions Suppresses the source-position bookkeeping emitted around calls for error reporting. It has nothing to do with GC tracking.
[blockexit] functions The call does not return to its caller.

opaque is a type keyword, not a modifier. Writing it in a modifier list has no effect.

serial

[serial] on a class derives the serialization traits, so the type can be written to and read from any serializer:

[serial] class Settings {
    theme: string
    width: number
}

The derive covers scalars, nested structs, optionals, arrays, and enums, including types imported from another module.