-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjects_more.fun
More file actions
executable file
·58 lines (49 loc) · 1.13 KB
/
objects_more.fun
File metadata and controls
executable file
·58 lines (49 loc) · 1.13 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env fun
/*
* This file is part of the Fun programming language.
* https://fun-lang.xyz/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the Apache-2.0 license.
* https://opensource.org/license/apache-2-0
*/
// Objects as maps: nested fields, methods with explicit self
// A method to move a 2D point by dx, dy
fun move(p, x, y)
p["x"] = p["x"] + x
p["y"] = p["y"] + y
return 0
// Create a point directly and inspect fields
p = { "x": 1, "y": 2, "move": move }
print(p["x"]) // -> 1
print(p["y"]) // -> 2
// Update a field directly
p["x"] = 10
print(p["x"]) // -> 10
// Call method with explicit self
move(p, 3, -2)
print(p["x"]) // -> 13
print(p["y"]) // -> 0
// Nested object example
rect = {
"pos": { "x": 0, "y": 0 },
"size": { "w": 5, "h": 4 }
}
print(rect["pos"]["x"]) // -> 0
print(rect["size"]["w"]) // -> 5
// Mutate nested fields
rect["pos"]["x"] = rect["pos"]["x"] + 7
rect["size"]["h"] = rect["size"]["h"] + 1
print(rect["pos"]["x"]) // -> 7
print(rect["size"]["h"]) // -> 5
/* Expected output:
1
2
10
13
0
0
5
7
5
*/