Skip to content

Commit aa548f4

Browse files
authored
docs: fix functions in array/map example (#8695)
1 parent 0b777c6 commit aa548f4

File tree

5 files changed

+32
-4
lines changed

5 files changed

+32
-4
lines changed

doc/docs.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1700,14 +1700,15 @@ fn main() {
17001700
res := run(5, fn (n int) int {
17011701
return n + n
17021702
})
1703+
println(res) // "10"
17031704
// You can even have an array/map of functions:
17041705
fns := [sqr, cube]
1705-
println((10)) // "100"
1706+
println(fns[0](10)) // "100"
17061707
fns_map := map{
17071708
'sqr': sqr
17081709
'cube': cube
17091710
}
1710-
println((2)) // "8"
1711+
println(fns_map['cube'](2)) // "8"
17111712
}
17121713
```
17131714

vlib/v/ast/str.v

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,9 @@ pub fn (x Expr) str() string {
241241
if x.name.starts_with('${x.mod}.') {
242242
return util.strip_main_name('${x.name}($sargs)')
243243
}
244+
if x.mod == '' && x.name == '' {
245+
return x.left.str() + '($sargs)'
246+
}
244247
return '${x.mod}.${x.name}($sargs)'
245248
}
246249
CharLiteral {
@@ -273,6 +276,14 @@ pub fn (x Expr) str() string {
273276
InfixExpr {
274277
return '$x.left.str() $x.op.str() $x.right.str()'
275278
}
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+
}
276287
ParExpr {
277288
return '($x.expr)'
278289
}
@@ -321,7 +332,7 @@ pub fn (x Expr) str() string {
321332
return res.join('')
322333
}
323334
StringLiteral {
324-
return '"$x.val"'
335+
return "'$x.val'"
325336
}
326337
Type {
327338
return 'Type($x.typ)'

vlib/v/checker/tests/prefix_err.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ vlib/v/checker/tests/prefix_err.vv:9:6: error: cannot take the address of 10
3333
| ^
3434
10 | _ := &"Hi"
3535
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'
3737
8 | _ := &(get() + 1)
3838
9 | _ := &10
3939
10 | _ := &"Hi"

vlib/v/fmt/fmt.v

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1808,6 +1808,9 @@ pub fn (mut f Fmt) call_expr(node ast.CallExpr) {
18081808
f.write('$name')
18091809
}
18101810
}
1811+
if node.mod == '' && node.name == '' {
1812+
f.write(node.left.str())
1813+
}
18111814
f.write_generic_if_require(node)
18121815
f.write('(')
18131816
f.call_args(node.args)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
}

0 commit comments

Comments
 (0)