Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Struct methods do not accept func arguments #40

Closed
larrycinnabar opened this issue Sep 13, 2020 · 1 comment
Closed

Struct methods do not accept func arguments #40

larrycinnabar opened this issue Sep 13, 2020 · 1 comment

Comments

@larrycinnabar
Copy link

larrycinnabar commented Sep 13, 2020

type Client struct{}
func (c *Client) Foo(s string) string {
	return s
}

gval.Evaluate(`client.Foo("bar")`, map[string]interface{}{"client": c}) // works: does return "bar"

But

type Client struct{}
func (c *Client) Foo(s string, f func()) string {
        f()
	return s
}

gval.Evaluate(`client.Foo("bar", func(){})`, map[string]interface{}{"client": c}) // does not work

returns&errors.errorString{s:"parsing error: client.Foo(\"bar\", func(){})\t:1:25 - 1:26 unexpected \"{\" while scanning arguments expected \")\" or \",\""} a <nil>

@generikvault
Copy link
Collaborator

Yes gval cannot interprete go closures. That would require an entire golang interpreter.

But you can use closure parameter:

type Client struct{}
func (c *Client) Foo(s string, f func()) string {
        f()
	return s
}

gval.Evaluate(`client.Foo("bar", closure)`, map[string]interface{}{"client": c, "closure" : func(){}})

or closure return types:

type Client struct{}
func (c *Client) Foo(s string, f func()) string {
        f()
	return s
}

func (c *Client) MakeClosure() func(){
	return func(){}
}

gval.Evaluate(`client.Foo("bar", client.MakeClosure())`, map[string]interface{}{"client": c} )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants