v / examples
Raw file | 32 loc (22 sloc) | 732 bytes | Latest commit hash 1c8074188
1// Function signatures can be declared as types:
2
3type Filter = fn (string) string
4
5// Functions can accept function types as arguments:
6
7fn filter(s string, f Filter) string {
8 return f(s)
9}
10
11// Declare a function with a matching signature:
12
13fn uppercase(s string) string {
14 return s.to_upper()
15}
16
17fn main() {
18 // A function can be assigned to a matching type:
19
20 my_filter := Filter(uppercase)
21
22 // You don't strictly need the `Filter` cast - it's only used
23 // here to illustrate how these types are compatible.
24
25 // All of the following prints "HELLO WORLD":
26
27 println(filter('Hello world', my_filter))
28 println(filter('Hello world', uppercase))
29 println(filter('Hello world', fn (s string) string {
30 return s.to_upper()
31 }))
32}