Version: 1.1.0
Parameterized declarations
A parameterized declaration is a function or struct that works for a family of types. Constraints tell the compiler what that family must support.
Essentials
- Put parameters in brackets and arguments in parentheses.
- Constrain a type parameter with a trait.
whereclauses add boolean conditions on parameters.- The body can only use operations the constraints promise.
- Call sites infer parameters when the arguments make them obvious.
Example
fn first[T: Movable](items: List[T]) -> T:
return items[0]
def main():
var nums = List[Int](4, 5, 6)
print(first(nums))
Keep in mind
If inference fails, pass the parameter explicitly. The error usually means two types could fit.