docs: fix functions in array/map example (#8695)
parent
0b777c68c3
commit
aa548f45ea
|
@ -1700,14 +1700,15 @@ fn main() {
|
||||||
res := run(5, fn (n int) int {
|
res := run(5, fn (n int) int {
|
||||||
return n + n
|
return n + n
|
||||||
})
|
})
|
||||||
|
println(res) // "10"
|
||||||
// You can even have an array/map of functions:
|
// You can even have an array/map of functions:
|
||||||
fns := [sqr, cube]
|
fns := [sqr, cube]
|
||||||
println((10)) // "100"
|
println(fns[0](10)) // "100"
|
||||||
fns_map := map{
|
fns_map := map{
|
||||||
'sqr': sqr
|
'sqr': sqr
|
||||||
'cube': cube
|
'cube': cube
|
||||||
}
|
}
|
||||||
println((2)) // "8"
|
println(fns_map['cube'](2)) // "8"
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
@ -241,6 +241,9 @@ pub fn (x Expr) str() string {
|
||||||
if x.name.starts_with('${x.mod}.') {
|
if x.name.starts_with('${x.mod}.') {
|
||||||
return util.strip_main_name('${x.name}($sargs)')
|
return util.strip_main_name('${x.name}($sargs)')
|
||||||
}
|
}
|
||||||
|
if x.mod == '' && x.name == '' {
|
||||||
|
return x.left.str() + '($sargs)'
|
||||||
|
}
|
||||||
return '${x.mod}.${x.name}($sargs)'
|
return '${x.mod}.${x.name}($sargs)'
|
||||||
}
|
}
|
||||||
CharLiteral {
|
CharLiteral {
|
||||||
|
@ -273,6 +276,14 @@ pub fn (x Expr) str() string {
|
||||||
InfixExpr {
|
InfixExpr {
|
||||||
return '$x.left.str() $x.op.str() $x.right.str()'
|
return '$x.left.str() $x.op.str() $x.right.str()'
|
||||||
}
|
}
|
||||||
|
MapInit {
|
||||||
|
mut pairs := []string{}
|
||||||
|
for ik, kv in x.keys {
|
||||||
|
mv := x.vals[ik].str()
|
||||||
|
pairs << '$kv: $mv'
|
||||||
|
}
|
||||||
|
return 'map{ ${pairs.join(' ')} }'
|
||||||
|
}
|
||||||
ParExpr {
|
ParExpr {
|
||||||
return '($x.expr)'
|
return '($x.expr)'
|
||||||
}
|
}
|
||||||
|
@ -321,7 +332,7 @@ pub fn (x Expr) str() string {
|
||||||
return res.join('')
|
return res.join('')
|
||||||
}
|
}
|
||||||
StringLiteral {
|
StringLiteral {
|
||||||
return '"$x.val"'
|
return "'$x.val'"
|
||||||
}
|
}
|
||||||
Type {
|
Type {
|
||||||
return 'Type($x.typ)'
|
return 'Type($x.typ)'
|
||||||
|
|
|
@ -33,7 +33,7 @@ vlib/v/checker/tests/prefix_err.vv:9:6: error: cannot take the address of 10
|
||||||
| ^
|
| ^
|
||||||
10 | _ := &"Hi"
|
10 | _ := &"Hi"
|
||||||
11 | _ := &"${b}"
|
11 | _ := &"${b}"
|
||||||
vlib/v/checker/tests/prefix_err.vv:10:6: error: cannot take the address of "Hi"
|
vlib/v/checker/tests/prefix_err.vv:10:6: error: cannot take the address of 'Hi'
|
||||||
8 | _ := &(get() + 1)
|
8 | _ := &(get() + 1)
|
||||||
9 | _ := &10
|
9 | _ := &10
|
||||||
10 | _ := &"Hi"
|
10 | _ := &"Hi"
|
||||||
|
|
|
@ -1808,6 +1808,9 @@ pub fn (mut f Fmt) call_expr(node ast.CallExpr) {
|
||||||
f.write('$name')
|
f.write('$name')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if node.mod == '' && node.name == '' {
|
||||||
|
f.write(node.left.str())
|
||||||
|
}
|
||||||
f.write_generic_if_require(node)
|
f.write_generic_if_require(node)
|
||||||
f.write('(')
|
f.write('(')
|
||||||
f.call_args(node.args)
|
f.call_args(node.args)
|
||||||
|
|
|
@ -0,0 +1,13 @@
|
||||||
|
fn sqr(n int) int {
|
||||||
|
return n * n
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
fns := [sqr]
|
||||||
|
println(fns[0](10))
|
||||||
|
fns_map := map{
|
||||||
|
'sqr': sqr
|
||||||
|
}
|
||||||
|
println(fns_map['sqr'])
|
||||||
|
println(fns_map['sqr'](2))
|
||||||
|
}
|
Loading…
Reference in New Issue