File tree Expand file tree Collapse file tree 5 files changed +32
-4
lines changed Expand file tree Collapse file tree 5 files changed +32
-4
lines changed Original file line number Diff line number Diff line change @@ -1700,14 +1700,15 @@ fn main() {
1700
1700
res := run(5, fn (n int) int {
1701
1701
return n + n
1702
1702
})
1703
+ println(res) // "10"
1703
1704
// You can even have an array/map of functions:
1704
1705
fns := [sqr, cube]
1705
- println((10)) // "100"
1706
+ println(fns[0] (10)) // "100"
1706
1707
fns_map := map{
1707
1708
'sqr': sqr
1708
1709
'cube': cube
1709
1710
}
1710
- println((2)) // "8"
1711
+ println(fns_map['cube'] (2)) // "8"
1711
1712
}
1712
1713
```
1713
1714
Original file line number Diff line number Diff line change @@ -241,6 +241,9 @@ pub fn (x Expr) str() string {
241
241
if x.name.starts_with ('${x.mod} .' ) {
242
242
return util.strip_main_name ('${x.name} ($sargs )' )
243
243
}
244
+ if x.mod == '' && x.name == '' {
245
+ return x.left.str () + '($sargs )'
246
+ }
244
247
return '${x.mod} .${x.name} ($sargs )'
245
248
}
246
249
CharLiteral {
@@ -273,6 +276,14 @@ pub fn (x Expr) str() string {
273
276
InfixExpr {
274
277
return '$x.left.str () $x.op.str () $x.right.str ()'
275
278
}
279
+ MapInit {
280
+ mut pairs := []string {}
281
+ for ik, kv in x.keys {
282
+ mv := x.vals[ik].str ()
283
+ pairs << '$kv : $mv '
284
+ }
285
+ return 'map{ ${pairs.join(' ')} }'
286
+ }
276
287
ParExpr {
277
288
return '($x.expr )'
278
289
}
@@ -321,7 +332,7 @@ pub fn (x Expr) str() string {
321
332
return res.join ('' )
322
333
}
323
334
StringLiteral {
324
- return '" $x.val "'
335
+ return "' $x.val '"
325
336
}
326
337
Type {
327
338
return 'Type($x.typ )'
Original file line number Diff line number Diff line change @@ -33,7 +33,7 @@ vlib/v/checker/tests/prefix_err.vv:9:6: error: cannot take the address of 10
33
33
| ^
34
34
10 | _ := &"Hi"
35
35
11 | _ := &"${b}"
36
- vlib/v/checker/tests/prefix_err.vv:10:6: error: cannot take the address of "Hi"
36
+ vlib/v/checker/tests/prefix_err.vv:10:6: error: cannot take the address of 'Hi'
37
37
8 | _ := &(get() + 1)
38
38
9 | _ := &10
39
39
10 | _ := &"Hi"
Original file line number Diff line number Diff line change @@ -1808,6 +1808,9 @@ pub fn (mut f Fmt) call_expr(node ast.CallExpr) {
1808
1808
f.write ('$name ' )
1809
1809
}
1810
1810
}
1811
+ if node.mod == '' && node.name == '' {
1812
+ f.write (node.left.str ())
1813
+ }
1811
1814
f.write_generic_if_require (node)
1812
1815
f.write ('(' )
1813
1816
f.call_args (node.args)
Original file line number Diff line number Diff line change
1
+ fn sqr(n int) int {
2
+ return n * n
3
+ }
4
+
5
+ fn main() {
6
+ fns := [sqr]
7
+ println(fns[0](10))
8
+ fns_map := map{
9
+ 'sqr': sqr
10
+ }
11
+ println(fns_map['sqr'])
12
+ println(fns_map['sqr'](2))
13
+ }
You can’t perform that action at this time.
0 commit comments