Skip to content

compare pointer and a string #490

Closed
Closed
@gosom

Description

@gosom

Thanks for the great library.

I experience a "strange" behavior.

package main

import (
	"fmt"

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

type Env struct {
	A string
	B *string
}

func main() {
	code := `A == B`

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

	value := "ok"
	env := Env{
		A: value,
		B: &value,
	}

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

	fmt.Println(output)
}

Try in go playground .

I expect the output to be true but it is false.

Is this behavior intentional?
Reading another issue related to pointers I was with the impression that pointers
should be "hidden", since a lot of stakeholders are people who cannot code well.

changing the code to:

code := `A == (B ?? "")`

outputs true as expected.

Maybe this at least can be documented (if it's not already and I missed it) or
the comparison operator supports a comparison between strings and pointers to strings.

best

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions