Type System
OSC2 is a strongly and statically typed language. Every variable and expression has a deterministic type at compile time.
Primitive Types
| Type | Description | Examples |
|---|---|---|
bool | Boolean value | true, false |
int | Signed 64-bit integer | 42, -7, 0x0539 |
uint | Unsigned 64-bit integer | 42u |
float | IEEE 754 double-precision float | 3.14, 1.0e4 |
string | Unicode 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 Quantity | Units |
|---|---|
| Length | nm, mm, cm, m, km |
| Time | ms, s, min, h |
| Speed | mps, kph/kmh, mph |
| Acceleration | mpss, kmphps |
| Angle | deg, rad |
| Mass | g, kg, ton |
| Temperature | K, 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