Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

how to execute a function with no return value #634

Answered by antonmedv
carmel asked this question in Q&A
Discussion options

I want to use expr to execute a function with no return value, for example to create a file with it, can you provide an example of this? I've been googling for a long time but I can't find any information about it, thanks in advance!

The following code will panic with unexpected token Operator:

func TestDocGen(t *testing.T) {
	// "github.com/carmel/gooxml/document"
	env := document.New()
	code := `
	let para = AddParagraph();
	let run = para.AddRun();
	run.AddText("document title");
	SaveToFile("test.docx")
	`
	program, err := expr.Compile(code, expr.Env(env))
	if err != nil {
		panic(err)
	}
	output, err := expr.Run(program, env)
	if err != nil {
		panic(err)
	}
	fmt.Println(output)
}
You must be logged in to vote

Yes, right now, it is not possible to do that. As Expr is only for expressions, and all expressions should return a value.

You can work around this restriction by returning something from functions and discarding the results. For example return true and you can this:

let para = AddParagraph();
let run = para.AddRun();
run.AddText("document title")
and SaveToFile("test.docx")

Or wrap in array:

[
 run.AddText("document title"),
 and SaveToFile("test.docx"),
]

I'm thinking to add an option to allow calling functions as statements. Also, I'm thinking of adding if/else into the language to the already existing cond ? yes : no.

Replies: 1 comment 1 reply

Comment options

Yes, right now, it is not possible to do that. As Expr is only for expressions, and all expressions should return a value.

You can work around this restriction by returning something from functions and discarding the results. For example return true and you can this:

let para = AddParagraph();
let run = para.AddRun();
run.AddText("document title")
and SaveToFile("test.docx")

Or wrap in array:

[
 run.AddText("document title"),
 and SaveToFile("test.docx"),
]

I'm thinking to add an option to allow calling functions as statements. Also, I'm thinking of adding if/else into the language to the already existing cond ? yes : no.

You must be logged in to vote
1 reply
Comment options

Thanks for the reply. Got it.

But the fact is that modifying function return value types can be disruptive to existing api. So it's looking forward to allow calling functions as statements feature.

Answer selected by carmel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants

AltStyle によって変換されたページ (->オリジナル) /