-
-
Couldn't load subscription status.
- Fork 463
-
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) }
Beta Was this translation helpful? Give feedback.
All reactions
-
👀 1
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
-
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.
Beta Was this translation helpful? Give feedback.
All reactions
-
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.
Beta Was this translation helpful? Give feedback.
All reactions
-
👀 1