expr-lang / expr

Expression language and expression evaluation for Go
https://expr-lang.org
MIT License
6.33k stars 406 forks source link

Typed int with ref equality #730

Open antonmedv opened 4 days ago

antonmedv commented 4 days ago
          Can I get the pointer value of the struct Env?

Playground:https://go.dev/play/p/WIj3_3NoR2z

package main

import (
    "fmt"

    "github.com/expr-lang/expr"
)

type ModeEnum int

const (
    ModeEnumA ModeEnum = 1
    ModeEnumB ModeEnum = 2
)

type Env struct {
    Mode *ModeEnum
}

func main() {
    code := `Mode == 1`

    tmp := ModeEnumA

    env := map[string]any{
        "Mode": &tmp,
    }

    program, err := expr.Compile(code, expr.Env(env))
    if err != nil {
        panic(err)
    }

    output, err := expr.Run(program, env)
    if err != nil {
        panic(err)
    }

    fmt.Println(output)
}

It prints false, but the expectation is true

Originally posted by @Cassius0924 in https://github.com/expr-lang/expr/issues/154#issuecomment-2488321401

Cassius0924 commented 3 days ago

If it is repaired, should the Mode be wrapped with an int function? like:

    code := `int(Mode) == 1`

Or will Mode be converted implicitly?

    code := `Mode == 1`
Cassius0924 commented 3 days ago

Is it possible to add a parameter to control whether implicit conversion is performed?

antonmedv commented 3 days ago

The problem is with typed int field. Expr see it as an int and assumes no deref is needed. I think we can try to make type checker more advance.

Cassius0924 commented 3 days ago

Are you already fixing it?