1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
This commit is contained in:
d2verb 2019-07-15 19:33:18 +09:00 committed by Alexander Medvednikov
parent 0556f5fd4e
commit 916b13b286
3 changed files with 79 additions and 6 deletions

View File

@ -2303,10 +2303,12 @@ fn (p mut Parser) map_init() string {
if !p.table.known_type(val_type) { if !p.table.known_type(val_type) {
p.error('map init unknown type "$val_type"') p.error('map init unknown type "$val_type"')
} }
typ := 'map_$val_type'
p.register_map(typ)
p.gen('new_map(1, sizeof($val_type))') p.gen('new_map(1, sizeof($val_type))')
p.check(.lcbr) p.check(.lcbr)
p.check(.rcbr) p.check(.rcbr)
return 'map_$val_type' return typ
} }
// [1,2,3] // [1,2,3]

View File

@ -543,11 +543,21 @@ fn type_default(typ string) string {
} }
// Default values for other types are not needed because of mandatory initialization // Default values for other types are not needed because of mandatory initialization
switch typ { switch typ {
case 'int': return '0'
case 'string': return 'tos("", 0)'
case 'void*': return '0'
case 'byte*': return '0'
case 'bool': return '0' case 'bool': return '0'
case 'string': return 'tos("", 0)'
case 'i8': return '0'
case 'i16': return '0'
case 'i32': return '0'
case 'u8': return '0'
case 'u16': return '0'
case 'u32': return '0'
case 'byte': return '0'
case 'int': return '0'
case 'rune': return '0'
case 'f32': return '0.0'
case 'f64': return '0.0'
case 'byteptr': return '0'
case 'voidptr': return '0'
} }
return '{}' return '{}'
return '' return ''

View File

@ -73,3 +73,64 @@ fn test_large_map() {
//println(time.ticks() - ticks) //println(time.ticks() - ticks)
} }
fn test_various_map_value() {
mut m1 := map[string]int{}
m1['test'] = 1
assert m1['test'] == 1
mut m2 := map[string]string{}
m2['test'] = 'test'
assert m2['test'] == 'test'
mut m3 := map[string]i8{}
m3['test'] = i8(0)
assert m3['test'] == i8(0)
mut m4 := map[string]i16{}
m4['test'] = i16(0)
assert m4['test'] == i16(0)
mut m5 := map[string]i32{}
m5['test'] = i32(0)
assert m5['test'] == i32(0)
mut m6 := map[string]u8{}
m6['test'] = u8(0)
assert m6['test'] == u8(0)
mut m7 := map[string]u16{}
m7['test'] = u16(0)
assert m7['test'] == u16(0)
mut m8 := map[string]u32{}
m8['test'] = u32(0)
assert m8['test'] == u32(0)
mut m9 := map[string]bool{}
m9['test'] = true
assert m9['test'] == true
mut m10 := map[string]byte{}
m10['test'] = byte(0)
assert m10['test'] == byte(0)
mut m11 := map[string]f32{}
m11['test'] = f32(0.0)
assert m11['test'] == f32(0.0)
mut m12 := map[string]f64{}
m12['test'] = f64(0.0)
assert m12['test'] == f64(0.0)
mut m13 := map[string]rune{}
m13['test'] = rune(0)
assert m13['test'] == rune(0)
mut m14 := map[string]voidptr{}
m14['test'] = voidptr(0)
assert m14['test'] == voidptr(0)
mut m15 := map[string]byteptr{}
m15['test'] = byteptr(0)
assert m15['test'] == byteptr(0)
}