Typed Variables
OSL allows you to declare variables with specific types, providing type safety and better code clarity. This feature enables you to enforce that a variable or object property can only hold values of a specified type.
Syntax
For variables:
type variableName = value
For object properties:
type objectName.propertyName = value
Supported Types
OSL supports the following type annotations for variables:
string
- Text valuesnumber
- Numeric values (integers and decimals)boolean
- Logical values (true/false)array
- JSON arraysobject
- JSON objectsfunction
- Function objectsany
- Any type (default if no type is specified)
Examples
Basic Type Declarations
// Typed variable declarations
string name = "Alice"
number age = 30
boolean isActive = true
array items = [1, 2, 3]
object settings = { theme: "dark" }
// Attempting to assign the wrong type will cause an error
name = 42 // Error: Cannot assign number to string variable
Typing Object Properties
You can also type specific properties of objects:
// Create an object
user = {
name: "Bob",
age: 25,
active: true
}
// Type a property
string user.name = "Charlie" // Works fine
number user.age = 30 // Works fine
// This would cause an error
number user.name = 50 // Error: Cannot assign number to string property
Type Enforcement
Once a variable or property is typed, OSL enforces that type for all future assignments:
// Initial declaration with type
number score = 100
// Valid reassignments
score = 200 // Works fine
score = score + 50 // Works fine
// Invalid reassignments
score = "High" // Error: Cannot assign string to number variable
score = true // Error: Cannot assign boolean to number variable
Working with Functions
Typed variables work well with functions that expect specific types:
// Function that expects a number
def double(number val) (
return val * 2
)
// Using typed variables with functions
number value = 5
result = double(value) // Works fine
string text = "10"
result = double(text) // Error: Function expected number but got string
Type Conversion
If you need to change a value's type, you can use conversion methods:
string textValue = "42"
number numValue = textValue.toNum() // Convert string to number
number price = 19.99
string priceTag = price.toStr() // Convert number to string
Benefits of Typed Variables
Error Prevention - Catch type-related errors early
Code Clarity - Make your intentions clear about what type a variable should hold
Better Tooling Support - Enable better code completion and hints
Self-Documenting Code - Types serve as documentation for your variables
Improved Maintainability - Makes code easier to understand and modify
Notes
Type annotations are optional - you can mix typed and untyped variables
Type checking happens at runtime when assignments occur
Once a variable is typed, that type is enforced for the lifetime of the variable
Type annotations do not affect the variable's value, only what values it can accept
Using typed variables can help catch bugs early and make your code more robust
Last updated
Was this helpful?