1 | ## oils_failures_allowed: 0
|
2 |
|
3 | #### Simple Expr Closure
|
4 | shopt --set ysh:upgrade
|
5 |
|
6 | proc my-expr (; expr) {
|
7 | echo $[io->evalExpr(expr)]
|
8 | }
|
9 |
|
10 | proc p {
|
11 | var i = 42
|
12 | my-expr [i + 1]
|
13 | }
|
14 |
|
15 | p
|
16 |
|
17 | ## STDOUT:
|
18 | 43
|
19 | ## END
|
20 |
|
21 | #### Simple Block Closure
|
22 | shopt --set ysh:upgrade
|
23 |
|
24 | shopt --set ysh:upgrade
|
25 |
|
26 | proc my-expr (; ; ; block) {
|
27 | call io->eval(block)
|
28 | }
|
29 |
|
30 | proc p {
|
31 | var i = 42
|
32 | my-expr {
|
33 | echo $[i + 1]
|
34 | }
|
35 | }
|
36 |
|
37 | p
|
38 |
|
39 | ## STDOUT:
|
40 | 43
|
41 | ## END
|
42 |
|
43 | #### Expr Closures in a Loop !
|
44 | shopt --set ysh:upgrade
|
45 |
|
46 | proc task (; tasks, expr) {
|
47 | call tasks->append(expr)
|
48 | }
|
49 |
|
50 | func makeTasks() {
|
51 | var tasks = []
|
52 | var x = 'x'
|
53 | for __hack__ in (0 ..< 3) {
|
54 | var i = __hack__
|
55 | var j = i + 2
|
56 | task (tasks, ^"$x: i = $i, j = $j")
|
57 | }
|
58 | return (tasks)
|
59 | }
|
60 |
|
61 | var exprs = makeTasks()
|
62 | #= blocks
|
63 |
|
64 | for ex in (exprs) {
|
65 | var s = io->evalExpr(ex)
|
66 | echo $s
|
67 | }
|
68 |
|
69 | ## STDOUT:
|
70 | x: i = 0, j = 2
|
71 | x: i = 1, j = 3
|
72 | x: i = 2, j = 4
|
73 | ## END
|
74 |
|
75 | #### Block Closures in a Loop !
|
76 | shopt --set ysh:upgrade
|
77 |
|
78 | proc task (; tasks; ; b) {
|
79 | call tasks->append(b)
|
80 | }
|
81 |
|
82 | func makeTasks() {
|
83 | var tasks = []
|
84 | var x = 'x'
|
85 | for __hack__ in (0 ..< 3) {
|
86 | var i = __hack__
|
87 | var j = i + 2
|
88 | task (tasks) { echo "$x: i = $i, j = $j" }
|
89 | }
|
90 | return (tasks)
|
91 | }
|
92 |
|
93 | var blocks = makeTasks()
|
94 | #= blocks
|
95 |
|
96 | for b in (blocks) {
|
97 | call io->eval(b)
|
98 | }
|
99 |
|
100 | ## STDOUT:
|
101 | x: i = 0, j = 2
|
102 | x: i = 1, j = 3
|
103 | x: i = 2, j = 4
|
104 | ## END
|
105 |
|
106 |
|
107 | #### Explicit __invoke__ for "objects in a loop", not closures in a loop
|
108 | shopt --set ysh:upgrade
|
109 |
|
110 | var procs = []
|
111 | for i in (0 ..< 3) {
|
112 | proc __invoke__ (; self) {
|
113 | echo "i = $[self.i]"
|
114 | }
|
115 | var methods = Object(null, {__invoke__})
|
116 | var obj = Object(methods, {i})
|
117 | call procs->append(obj)
|
118 | }
|
119 |
|
120 | for p in (procs) {
|
121 | p
|
122 | }
|
123 |
|
124 | # TODO: sugar
|
125 | # proc p (; self) capture {i} {
|
126 | # echo "i = $[self.i]"
|
127 | # }
|
128 | # call procs->append(p)
|
129 |
|
130 | ## STDOUT:
|
131 | i = 0
|
132 | i = 1
|
133 | i = 2
|
134 | ## END
|
135 |
|
136 |
|
137 | #### Expr Closures in a different module
|
138 | shopt --set ysh:upgrade
|
139 |
|
140 | use $REPO_ROOT/spec/testdata/module2/closure.ysh --pick {local,global}_expr
|
141 |
|
142 | echo $[io->evalExpr(global_expr)]
|
143 |
|
144 | echo $[io->evalExpr(local_expr())]
|
145 |
|
146 | ## STDOUT:
|
147 | global!
|
148 | global local!
|
149 | ## END
|
150 |
|
151 |
|
152 | #### Command Closures in a different module
|
153 | shopt --set ysh:upgrade
|
154 |
|
155 | use $REPO_ROOT/spec/testdata/module2/closure.ysh --pick {local,global}_block
|
156 |
|
157 | call io->eval(global_block)
|
158 |
|
159 | call io->eval(local_block())
|
160 |
|
161 | ## STDOUT:
|
162 | global!
|
163 | global local!
|
164 | ## END
|
165 |
|
166 | #### Closures as default argument
|
167 | shopt --set ysh:upgrade
|
168 |
|
169 | use $REPO_ROOT/spec/testdata/module2/closure.ysh --pick default_{expr,block}
|
170 |
|
171 | echo $[io->evalExpr(default_expr())]
|
172 |
|
173 | call io->eval(default_block())
|
174 |
|
175 | ## STDOUT:
|
176 | global expr!
|
177 | global block!
|
178 | ## END
|
179 |
|