Write a generic function
Place type parameters after a function's name. moon normally infers them from arguments and context. A direct call can write function<Type>(...) when an empty collection or another argument supplies no clue.
The bare angle-bracket spelling is moon's explicit call form. It takes no dot or colon prefix.
fn identity<T>(value: T) -> T { value }
fn first<T>(values: [T]) -> Optional<T> {
if values.len() == 0 { none } else { some(values[0]) }
}
fn main() {
println(identity("moon"))
println(first<int>([7]))
}
Examples use the moon toolchain version shown beside each result.
Output & diagnostics
Expected: moon some(7)
Ctrl/⌘+Enter runs the current lesson.