mirror of
https://github.com/vlang/v.git
synced 2025-09-10 07:47:20 -04:00
parser: allow map cast syntax map[k]v(expr)
(#23401)
This commit is contained in:
parent
7078a2e185
commit
9fc83526aa
@ -2719,10 +2719,23 @@ fn (mut p Parser) name_expr() ast.Expr {
|
|||||||
if is_option {
|
if is_option {
|
||||||
map_type = map_type.set_flag(.option)
|
map_type = map_type.set_flag(.option)
|
||||||
}
|
}
|
||||||
return ast.MapInit{
|
node = ast.MapInit{
|
||||||
typ: map_type
|
typ: map_type
|
||||||
pos: pos
|
pos: pos
|
||||||
}
|
}
|
||||||
|
if p.tok.kind == .lpar {
|
||||||
|
// ?map[int]int(none) cast expr
|
||||||
|
p.check(.lpar)
|
||||||
|
expr := p.expr(0)
|
||||||
|
p.check(.rpar)
|
||||||
|
return ast.CastExpr{
|
||||||
|
typ: map_type
|
||||||
|
typname: p.table.sym(map_type).name
|
||||||
|
expr: expr
|
||||||
|
pos: pos.extend(p.tok.pos())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return node
|
||||||
}
|
}
|
||||||
// `chan typ{...}`
|
// `chan typ{...}`
|
||||||
if p.tok.lit == 'chan' {
|
if p.tok.lit == 'chan' {
|
||||||
|
25
vlib/v/tests/map_cast_test.v
Normal file
25
vlib/v/tests/map_cast_test.v
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
fn foo() map[int]int {
|
||||||
|
return {
|
||||||
|
1: 2
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_main() {
|
||||||
|
a := ?map[int]int(none)
|
||||||
|
assert a == none
|
||||||
|
|
||||||
|
b := ?map[int]int({
|
||||||
|
1: 2
|
||||||
|
})
|
||||||
|
assert b?[1] == 2
|
||||||
|
|
||||||
|
c := ?map[int]map[string]string({
|
||||||
|
1: {
|
||||||
|
'foo': 'bar'
|
||||||
|
}
|
||||||
|
})
|
||||||
|
assert c?[1]['foo'] == 'bar'
|
||||||
|
|
||||||
|
d := ?map[int]int(foo())
|
||||||
|
assert d?[1] == 2
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user