-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeneric_method_dispatch.ko
More file actions
36 lines (31 loc) · 901 Bytes
/
generic_method_dispatch.ko
File metadata and controls
36 lines (31 loc) · 901 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Generic method dispatch — method calls on user-defined structs.
//
// Demonstrates inherent impl blocks with methods that accept
// closures as parameters for flexible dispatch.
//
// Compile and run:
// kodoc build examples/generic_method_dispatch.ko -o generic_method_dispatch
// ./generic_method_dispatch
module generic_method_dispatch {
meta {
purpose: "Demonstrate method dispatch for generic types"
version: "0.1.0"
}
struct Container {
value: Int
}
impl Container {
fn get_value(self) -> Int {
return self.value
}
fn apply(self, f: (Int) -> Int) -> Int {
return f(self.value)
}
}
fn main() -> Int {
let c: Container = Container { value: 42 }
let v: Int = c.get_value()
let doubled: Int = c.apply(|x: Int| -> Int { x * 2 })
return 0
}
}