We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
package main import ( "errors" "fmt" "reflect" "github.com/expr-lang/expr" ) type Number interface { int | uint | uint32 | int32 | uint64 | int64 | float32 | float64 } func TryStrToNum[T Number](str string) (retValue T, err error) { program, err := expr.Compile(str, expr.AsKind(reflect.TypeOf(retValue).Kind())) if err != nil { fmt.Printf("Compile Error: %s, %v\n", str, err) return } v, err := expr.Run(program, nil) if err != nil { fmt.Printf("Run Error: %s, %v\n", str, err) return } retValue, flag := v.(T) if !flag { fmt.Printf("StrToNumber Error: %s, %v", str, v) return retValue, errors.New(fmt.Sprintf("TryStrToNum error: %s", str)) } return retValue, nil } func main() { fmt.Println(TryStrToNum[int32]("1")) } /* Compile Error: 1, expected int32, but got int 0 expected int32, but got int */
is it a bug ? How to fix it?
The text was updated successfully, but these errors were encountered:
What is reflect type of reflect.TypeOf(retValue).Kind()?
I suspect it is an interface of any?
Sorry, something went wrong.
@antonmedv
when I use: TryStrToNum[int32]("1") reflect.TypeOf(retValue).Kind() is int32
TryStrToNum[int32]("1")
int32
I will take a look.
hi,Would you mind if I fixed this issue link
Successfully merging a pull request may close this issue.
is it a bug ?
How to fix it?
The text was updated successfully, but these errors were encountered: