ufff
This commit is contained in:
parent
c8b8dd820e
commit
0e7fffcfe0
67 changed files with 154 additions and 65 deletions
49
examples/objects_more.fun
Normal file
49
examples/objects_more.fun
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
#!/usr/bin/env fun
|
||||
|
||||
// Objects as maps: nested fields, methods with explicit self
|
||||
|
||||
// A method to move a 2D point by dx, dy
|
||||
fun move(self, dx, dy)
|
||||
self["x"] = self["x"] + dx
|
||||
self["y"] = self["y"] + dy
|
||||
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
|
||||
*/
|
||||
Loading…
Add table
Add a link
Reference in a new issue