Types
OSL can keep values dynamic or check them against declared types. Types affect diagnostics, method resolution, generated Go, and package-handle calls.
Core types
string
"hello"
int
42
number
42.5
boolean or bool
true
array
[1, "two"]
object
{name: "Ada"}
any
Any runtime value
null
null
An integer literal has type int. A decimal literal has type number.
Nullable values
Append ? when a value may be null:
string? nickname = null
def findName(string id) string? (
if id == "owner" return "Ada"
return null
)A trailing nullable function parameter may be omitted. A nullable parameter before a required parameter still has to be passed.
Typed arrays and maps
T[] is a growable typed array:
T[size] is a fixed-size array. Omitted elements receive the type's zero value, and methods that change the length are compile errors.
key[value] describes a typed map:
Nested forms are allowed:
Package handle types use the package name. Pointer handles start with *:
Assertions
Use .assert(type) when a dynamic value must have a specific runtime type:
The generic shorthand is equivalent:
.assertElse(type, fallback) returns the fallback after a mismatch. When the fallback has an unambiguous type, omit the type argument:
The compiler warns about assertions it can prove redundant and rejects assertions it can prove impossible.
Narrowing
A typeof comparison narrows an any or union value inside the matching branch:
Unions and aliases
Join accepted types with | and name repeated types with type:
Conversion
The most common conversions are methods:
Conversion is different from assertion. Conversion attempts to produce another representation. Assertion checks the existing runtime type.
Last updated