Typed Parameters
In OSL, you can specify the expected type of a function parameter to improve code clarity and enable better error checking. This feature allows you to declare what type of data a function expects, making your code more robust and self-documenting.
Syntax
Supported Types
You can use any of the following types for parameter type annotations:
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 Annotations
Using Type Annotations for Validation
When you specify a type for a parameter, OSL will automatically validate that the provided argument matches the expected type. If an incorrect type is provided, an error will be thrown.
Complex Type Annotations
You can also use type annotations with more complex function signatures:
Benefits of Typed Parameters
Self-documenting code - Type annotations make it clear what kind of data a function expects
Early error detection - Type mismatches are caught when the function is called
Better IDE support - Enables better code completion and hints
Improved maintainability - Makes code easier to understand and modify
Notes
Type annotations are optional - you can mix typed and untyped parameters
If no type is specified, the parameter accepts any type
Type checking happens at runtime when the function is called
Type annotations do not affect the function's return value
Last updated
Was this helpful?