Skip to content

foo.bar - method or key value? #850

@guamoko995

Description

@guamoko995

Good day everyone!

I might be wrong, but it seems to me that method precedence over keyed value is not documented anywhere. And I'm not even sure if this behavior is stable. Because of this, I don't think foo.bar should be equivalent to foo[bar].

In Go Playground

package main

import (
	"fmt"

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

type mapWithMethod map[string]func() string

func (m mapWithMethod) GetString() string {
	return "thise is from method"
}

func GetString() string {
	return "thise is from func"
}

type env struct {
	M mapWithMethod
}

func main() {
	env := env{
		M: mapWithMethod{"GetString": GetString},
	}
	prog, _ := expr.Compile(`M.GetString()`, expr.Env(env), expr.Optimize(true))

	result, _ := expr.Run(prog, env)

	fmt.Println(result)
	//Output: thise is from method
}

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