NATIVE | Language | Program

The NATIVE keyword lets you specify platform-specific code for a particular target.

The grammar of a single NATIVE line:

A NATIVE keyword can live inside FUNCTION , TYPE , and MODULE keywords, or at the Root.

Provides that function's platform-specific implementation for one target.

Each line is the function's body on that target, given verbatim in its own idiom — so the same union is the dotted Code.Language.Json.Value in Swift and JavaScript but the _ -joined Code_Language_Json_Value in Dart. Anything the function provides — its INPUT and HOLE parameters, and any generic type — is inserted into the native code wrapped in {{ }} around its name, as with the {{input}} above, so the compiler can process it before splicing it in. A Program body, where present, is the fallback for any target with no NATIVE line.

Gives that type's native representation — the host type used in place of a Program record.

Dictionary is generic over one type-checked argument, ValueType , with keys always Text . Each NATIVE line inserts {{ValueType}} where the value type belongs, and the compiler lowers it generically, substituting each target's own host type — so Dictionary<Text> becomes Map<String, String> in Dart and [String: String] in Swift. A type may also mix NATIVE lines with FIELD or OPTION members as the fallback.

Runs per-target setup once, when the module is first used in a program.

Tied to no single definition, it prepares the host environment — an import, a global — that the module's declarations rely on.

Runs always, as part of the program itself — setup tied to no module.

With no enclosing definition, the line runs as part of the program itself, in source order.

In every case the dart , swift and js rows are required; nodejs , ts , go , rust , zig , glsl and php may follow. TypeScript needs no row of its own: the js body is valid TypeScript, and is reused unless a ts row overrides it.

Nothing. A NATIVE is a leaf: a target name and a verbatim string. It has no body and opens no indented lines — the whole line is one target's host code.

The same example, rendered to each target.

A function with only NATIVE lines renders to just the targets it gives a body for. This ToString has no Program body to fall back on, so Go, Rust and Zig each need their own NATIVE line before they can be built.

A NATIVE line is one target's body for a FUNCTION ; the function's parameters are INPUT and HOLE . A function written purely in Program has a body of LET bindings instead, and one may mix the two — native lines with a Program body as the fallback. The enclosing function lives at the Root or inside a MODULE . Back to the language .

Recommended articles