Skip to content

question about overload condition operator == #818

@ForeverCzz

Description

@ForeverCzz

I was trying to overload operator == to support json.Number. And I wrote this:

type Filter interface {
// ...
}

type exprFilter struct {
	p *vm.Program
}

type FilterEnv struct {
	JsonNumEq func(a json.Number, b any) bool
}

// NewFilter creates a filter
func NewFilter(config string) (Filter, error) {
	f := exprFilter{}
	if config == "" {
		return f, nil
	}
	opts := []expr.Option{
		expr.Env(FilterEnv{
			JsonNumEq: func(a json.Number, b any) bool {
				return fmt.Sprint(a) == fmt.Sprint(b)
			},
		}),
		expr.Operator("==", "JsonNumEq"),
	}

	p, err := expr.Compile(config, opts...)
	if err != nil {
		return f, err
	}
	f.p = p
	return f, nil
}

// ...
filter, err := NewFilter("status==4")
if err != nil { log.Fatal(err) }
// ...

and I got this:

unknown name status (1:1)
 | status==4
 | ^
  1. is it possible to overload operator == with two operands with different types?
  2. what wrong leads to my filter creation failure?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions