Skip to content

Macros as Functions

revusky edited this page Nov 4, 2023 · 4 revisions

A macro can now be used as it it was a function (or method)

Consider a simple little macro:

[#macro hello name]Hello, ${name}![/#macro]

If you wanted to capture the output of the macro and store it in a variable, you could do something like:

[#assign hellovar][@hello client.name/][/#assign]

Now, you can write equivalently:

#var hellovar = hello(client.name)

(Note that the above line uses the newer var directive. It also is somewhat cleaner looking because it uses the newer terse syntax.)

By the way, this bit of syntactic sugar has been available since July 2020 in my local version, i.e. the one use in Congo development. I implemented this for use in CongoCC (JavaCC 21 at the time) internal development. In any case, I think it corresponds to the principle of least surprise. The attempt to use a macro invocation as if it was a method/function just leads to the macro being invoked and the output being captured. And, generally speaking, it looks preferable to write:

${someMethod(hello(someName))}

rather than:

[#assign intermediateVar][@hello someName/][/#assign]
${someMethod(intermediateVar)}
Clone this wiki locally