Skip to content

Cannot use map[string]interface {} as argument (type main.Env) to call dump #694

Open
@vmihailenco

Description

@vmihailenco

The following program incorrectly assumes that the type of $env is map[string]any, although args[0] holds Env:

package main

import (
	"fmt"
	"time"

	"github.com/davecgh/go-spew/spew"
	"github.com/expr-lang/expr"
)

type Env struct {
}

func (Env) Dump(args ...any) (any, error) {
	spew.Dump(args)
	return nil, nil
}

func main() {
	code := `dump($env)`

	env := Env{}
	program, err := expr.Compile(code,
		expr.Function("dump", env.Dump, new(func(Env) any)),
		//expr.Function("dump", env.Dump, new(func(any) any)),
		expr.Env(env)) // Pass the struct as an environment.
	if err != nil {
		panic(err)
	}

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

	fmt.Print(output)
}

Changing expr.Function("dump", env.Dump, new(func(Env) any)) to expr.Function("dump", env.Dump, new(func(any) any)) will allow to compile the program and inspect the type of the arg.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions