Skip to main content

Type System

OSC2 is a strongly and statically typed language. Every variable and expression has a deterministic type at compile time.

Primitive Types

TypeDescriptionExamples
boolBoolean valuetrue, false
intSigned 64-bit integer42, -7, 0x0539
uintUnsigned 64-bit integer42u
floatIEEE 754 double-precision float3.14, 1.0e4
stringUnicode string"hello", 'world'

Enumerations

Use enum to define an enumeration type:

enum color: [red, green, blue]
enum status: [active = 1, inactive = 0]

my_color: color = color!red

Physical Types and Units

OSC2 has a built-in physical quantity system based on the SI international system of units:

type speed is SI(m: 1, s: -1)
unit kmh of speed is SI(m: 1, s: -1, factor: 0.27777777778)

Common physical types can be used directly:

distance: length = 100m
duration: time = 30s
v: speed = 60kph
acc: acceleration = 2.0 mpss
angle_val: angle = 45deg

Supported unit examples:

Physical QuantityUnits
Lengthnm, mm, cm, m, km
Timems, s, min, h
Speedmps, kph/kmh, mph
Accelerationmpss, kmphps
Angledeg, rad
Massg, kg, ton
TemperatureK, C, F

Structs

Use struct to define a composite type:

struct point_2d:
x: length
y: length

Actors

An actor is a participant in a scenario. Declare one with actor:

actor vehicle:
length: length
width: width
max_speed: speed
color: string

Inheritance

A type can inherit from another:

struct truck inherits vehicle:
cargo_weight: mass

Extensions

Use extend to add new fields to an existing type:

extend vehicle: is_electric: bool

Lists

colors: list of string = ["red", "green", "blue"]
first: string = colors[0]
count: int = colors.size()

Type Conversion

x: float = 42 # int → float implicit conversion
my_color: color = 1.as(color) # explicit conversion
is_vehicle: bool = obj.is(vehicle) # type check