PaesslerAG / gval

Expression evaluation in golang
BSD 3-Clause "New" or "Revised" License
731 stars 82 forks source link

Add option to override functions to parse string/float/bool #68

Open lafriks opened 2 years ago

lafriks commented 2 years ago

Currently when defining language there is no way to override how float/string/bool are handled and to override behavior for example to be more strict for operations not to allow autocasting for example false || 1 will result in 1 being treated as boolean true or 1+'1' resulting into 2

generikvault commented 2 years ago

Hello lafriks,

that's partially correct. The conversion is done by the Boolean Float operators. When you use only interface operators gval will not cast.

gval.InfixOperator("+", f func(a, b string) (interface{}, error){
numA, okA := a.(float64)
if !okA{
  return nil, yourError
}
...
return numA+numB, nil
}

When Go 1.18 is released I will do some experiments with generics that could lead to have a feature like this directly integrated into the language but I can't make any promises on this front.