Skip to content

foo.bar is not equivalent to foo["bar"] #851

@guamoko995

Description

@guamoko995

Good day everyone!
The documentation says that foo.bar and foo["bar"] are equivalent, but they're not. The parser doesn't expect foo["bar"] (), while it might expect foo.bar().

In Go Playground

package main

import (
	"fmt"

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

func main() {
	env := struct{ foo struct{ fn func() string } }{
		foo: struct{ fn func() string }{
			fn: func() string { return "baar" },
		},
	}

	_, err := expr.Compile(`foo.fn()`, expr.Env(env), expr.Optimize(true))
	fmt.Println(err)

	_, err = expr.Compile(`foo["fn"]()`, expr.Env(env), expr.Optimize(true))
	fmt.Println(err)

	// Output:
	// <nil>
	// unexpected token Bracket("(") (1:10)
	// | foo["fn"]()
	// | .........^
}

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