Map Access does not have check
I am trying to put a check in map access.
if you have a Golang map foo and it does not have an element "bar" the code below will fail silently without an error message regarding non existent map element. It returns a 0 and the silent nature of this substitution and error can create issues.
foo.bar+1
I suggest in line 37 of runtime.go:
case reflect.Map:
value := v.MapIndex(reflect.ValueOf(i))
if value.IsValid() {
if value.CanInterface() {
return value.Interface()
}
} else {
panic(fmt.Sprintf("%v not found", i)) // this is the addition
//elem := reflect.TypeOf(from).Elem() // this is commented
//return reflect.Zero(elem).Interface() // this is commented
}
Thank you.
I don’t remember the logic behind this behaviour. Maybe @davidklassen has some thoughts?
I don’t remember the logic behind this behaviour. Maybe @davidklassen has some thoughts?
@antonmedv ,
The above code fragment will fix it. runtime.go:37
Not just a fix. Expr will behave differently.
Not just a fix. Expr will behave differently.
@antonmedv,
Understood. However it will be in the line of other checks made such as a slice index out of bounds error. Etc.
bar not found (1:5)
| foo.bar+1
| ....^
Regardless .. excellent work with the rest.
Respectfully.
Added a such check, fixed in master.