[
https://issues.apache.org/jira/browse/FREEMARKER-107?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16952274#comment-16952274
]
Pascal Proulx commented on FREEMARKER-107:
------------------------------------------
Well, in my case, the parameter defaults defined in m1 and m2 would have been
included in my use of the catch-all "args", because they were merged manually
with some defaults using a custom pattern (not shown above, but mentioned
earlier: e.g. <#local args = \{"a":1, "b":2} + args>). In that sense I would
have expected .args to contain the defaults defined in m1 and m2, and would
have assumed that foo and .args.foo would have given the same result. The place
where defaults would not have applied is in the spread_args call to m3, where
most of the default parameters on m3 (except those not defined in m1 and m2)
were overridden by the args (m3 basically implements the interface of m1 or m2,
but might have extra parameters, or fewer).
The behavior you describe might be useful in a different case - but memory
eludes me as to where that was or would be.
I guess I should have made a better example with the original pattern, which I
use more like this, with default value emulation:
{code:java}
<#macro m1 args...><#local args = {"a":1, "b":2} + args><#-- tons of code
-->${args.a}<#-- tons of code --><@m3?spread_args(args) e=3/><#-- tons of code
--></#macro>{code}
Sorry if that changes things or was unclear.
> Hash expansion to macro arguments (Python **kwargs style)
> ---------------------------------------------------------
>
> Key: FREEMARKER-107
> URL: https://issues.apache.org/jira/browse/FREEMARKER-107
> Project: Apache Freemarker
> Issue Type: New Feature
> Components: engine
> Affects Versions: 2.3.28
> Reporter: Pascal Proulx
> Priority: Major
>
> Hello,
> We heavily rely on Freemarker macros to build a helper template API, but have
> had to make large workarounds for passing contents of hashes as macro
> arguments, for several years. (In truth I should have made this ticket much
> sooner!)
> It would help greatly simplify our work to have hash expansion to macro
> arguments, like this:
> {code:java}
> <#macro myMacro arg1 arg2 arg3="value3">...</#macro>
> <#assign myHash = {"arg1":"value1", "arg2":"value2"}>
> <@myMacro **myHash/><#-- the hash contents are passed as parameters, instead
> of the hash itself -->
> {code}
> This exists in Python:
> {code:java}
> def test_var_args_call(arg1, arg2, arg3):
> pass
> kwargs = {"arg3": 3, "arg2": "two"}
> test_var_args_call(1, **kwargs)
> {code}
> Essentially the hash contents fill in any arguments not explicitly specified.
> For the case where arguments are specified in addition to the hash, you may
> need to decide on a good syntax, e.g.:
> {code:java}
> <@myMacro arg1="value1" **myHash/>{code}
> This example doesn't have much precedent in freemarker syntax but is fairly
> understandable.
> Although we don't need it nearly as much, the same could be done with lists
> and function arguments:
> {code:java}
> <#function myFunc arg1 arg2>...</#function>
> <#assign myList = ["val1", "val2"]>
> ${myFunc(*myList)}
> <#assign myList = ["val2"]>
> ${myFunc("val1", *myList)}
> {code}
> Again similar to Python:
> {code:java}
> def test_var_args_call(arg1, arg2, arg3):
> pass
> args = ("two", 3)
> test_var_args_call(1, *args)
> {code}
> You might want this for consistency, although in practice the hash expansion
> will be many times more useful to us.
> If there's a lack a manpower I could try to see what I can do digging into
> the source, but wanted to bring this up for discussion first. It doesn't
> appear hard to implement to dump a hash into the macro args map, but there is
> defining the syntax.
> We use Freemarker 2.3.28 at the moment.
> Thank you
--
This message was sent by Atlassian Jira
(v8.3.4#803005)