diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index da46ab86ed..0a39f11902 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -596,10 +596,16 @@ fn (mut f Fmt) expr(node ast.Expr) { ast.MapInit { if it.keys.len == 0 { if it.value_type == 0 { - f.write('map[string]int') // TODO + typ_sym := f.table.get_type_symbol(it.typ) + minfo := typ_sym.info as table.Map + mk := f.table.get_type_symbol(minfo.key_type).name + mv := f.table.get_type_symbol(minfo.value_type).name + f.write('map[${mk}]${mv}{}') return } - f.write('map[string]') + f.write('map[') + f.write(f.type_to_str(it.key_type)) + f.write(']') f.write(f.type_to_str(it.value_type)) return } @@ -1001,6 +1007,26 @@ fn expr_is_single_line(expr ast.Expr) bool { fn (mut f Fmt) array_init(it ast.ArrayInit) { if it.exprs.len == 0 && it.typ != 0 && it.typ != table.void_type { // `x := []string` + typ_sym := f.table.get_type_symbol(it.typ) + if typ_sym.kind == .array && typ_sym.name.starts_with('array_map') { + ainfo := typ_sym.info as table.Array + map_typ_sym := f.table.get_type_symbol( ainfo.elem_type ) + minfo := map_typ_sym.info as table.Map + mk := f.table.get_type_symbol(minfo.key_type).name + mv := f.table.get_type_symbol(minfo.value_type).name + for _ in 0..ainfo.nr_dims { + f.write('[]') + } + f.write('map[${mk}]${mv}') + f.write('{') + if it.has_cap { + f.write('cap: ') + f.expr(it.cap_expr) + } + f.write('}') + return + } + f.write(f.type_to_str(it.typ)) f.write('{') if it.has_cap { diff --git a/vlib/v/fmt/tests/maps_keep.vv b/vlib/v/fmt/tests/maps_keep.vv new file mode 100644 index 0000000000..b5737f7077 --- /dev/null +++ b/vlib/v/fmt/tests/maps_keep.vv @@ -0,0 +1,17 @@ +fn workaround() { + a := map[string]string{} + println(a) +} + +fn main() { + mut ams := []map[string]string{} + ams << { + 'a': 'b' + 'c': 'd' + } + ams << { + 'e': 'f' + 'g': 'h' + } + println(ams) +} diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 2692114c96..fa7c9364f2 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -602,6 +602,11 @@ pub fn (mut p Parser) name_expr() ast.Expr { // `map[string]int` initialization if p.tok.lit == 'map' && p.peek_tok.kind == .lsbr { map_type := p.parse_map_type() + if p.tok.kind == .lcbr && p.peek_tok.kind == .rcbr { + p.next() + p.next() + eprintln('-----------------------------------------') + } return ast.MapInit{ typ: map_type }