Wu Language
Search…
Functions
Wu functions are expressions, but can also be declared using the same notation as struct. For example we can declare functions like this:
1
add_two: fun(a: int) {
2
a += 2
3
}
Copied!
The following is just as valid, but more ugly:
1
add_two := fun(a: int) {
2
a += 2
3
}
Copied!
They are the exact same behind the scenes. No difference at all.
Functions return the last expression in their body implicitly(like in Rust) like so:
1
ten: fun {
2
10
3
}
4
5
print(ten()) # 10
Copied!
Why have parentheses if they're empty anyways?
You are still able to use explicit returns:
1
twenty: fun {
2
return ten() + ten()
3
}
Copied!
Of course, as we're dealing with a decent language, higher order functions are a thing:
1
apply: fun(f: fun(int) -> int, a: int) -> int {
2
f(a)
3
}
4
5
# 12
6
apply(fun(a: int) { a + 2 }, 10)
Copied!
Now for something vaguely interesting. Splats are basically a catchall parameter, that binds as many arguments you throw at it into an array:
1
choose_first: fun(things: ...) -> any {
2
things[0]
3
}
4
5
a := choose_first(1, 2, 3, 4, 5, 6)
6
7
print(a) # 1
Copied!
Of course we can type the splats:
1
choose_second: fun(floats: ...float) -> float {
2
floats[1]
3
}
4
5
a := choose_second(1.0, 2.1, 3.8)
6
7
print(a) # 2.1
Copied!
Last modified 2yr ago
Copy link