From notifications-return-6116-archive-asf-public=cust-asf.ponee.io@freemarker.apache.org Tue Oct 15 23:39:02 2019 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [207.244.88.153]) by mx-eu-01.ponee.io (Postfix) with SMTP id 54D11180651 for ; Wed, 16 Oct 2019 01:39:02 +0200 (CEST) Received: (qmail 18662 invoked by uid 500); 15 Oct 2019 23:39:01 -0000 Mailing-List: contact notifications-help@freemarker.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@freemarker.apache.org Delivered-To: mailing list notifications@freemarker.apache.org Received: (qmail 18642 invoked by uid 99); 15 Oct 2019 23:39:01 -0000 Received: from mailrelay1-us-west.apache.org (HELO mailrelay1-us-west.apache.org) (209.188.14.139) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 15 Oct 2019 23:39:01 +0000 Received: from jira-he-de.apache.org (static.172.67.40.188.clients.your-server.de [188.40.67.172]) by mailrelay1-us-west.apache.org (ASF Mail Server at mailrelay1-us-west.apache.org) with ESMTP id C6C50E2DE7 for ; Tue, 15 Oct 2019 23:39:00 +0000 (UTC) Received: from jira-he-de.apache.org (localhost.localdomain [127.0.0.1]) by jira-he-de.apache.org (ASF Mail Server at jira-he-de.apache.org) with ESMTP id 42B8078053F for ; Tue, 15 Oct 2019 23:39:00 +0000 (UTC) Date: Tue, 15 Oct 2019 23:39:00 +0000 (UTC) From: "Pascal Proulx (Jira)" To: notifications@freemarker.apache.org Message-ID: In-Reply-To: References: Subject: [jira] [Commented] (FREEMARKER-107) Hash expansion to macro arguments (Python **kwargs style) MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-JIRA-FingerPrint: 30527f35849b9dde25b450d4833f0394 [ https://issues.apache.org/jira/browse/FREEMARKER-107?page=3Dcom.atlas= sian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=3D= 16952382#comment-16952382 ]=20 Pascal Proulx commented on FREEMARKER-107: ------------------------------------------ I follow. The locals approach was what I meant when I mentioned .locals pre= viously, though that wasn't enough. (I know what you mean by "snapshots" as= well - I used a map copy util in some places instead of "+" operator, but = nevermind) I also imagined maybe you could use the "+" operator logic inter= nally to implement (since the .args, defaults and catch-alls should be read= -only?), but you know better. I suppose you could call it: .args_ex, .args_exp, args_all, .arg_exp, .arg_= expanded,=C2=A0.args_expanded, .args_exploded. It wouldn't be the end of the world if it didn't include the catch-alls, be= cause it's easy to concat with "+" (<@m3?spread_args(.arg_locals+extras)/>)= , but I can't think of anywhere I'd use it without the catch-alls myself. I have trouble picturing anywhere I'd use .args without the defaults, thoug= h it could be useful as a workaround to test if an argument was specified b= y the caller, "under" the defaults (not sure if that's possible currently).= I don't recall trying to do that in a programming language. If I think of anything I'll let you know. =C2=A0 =C2=A0 =C2=A0 > 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 a= rguments, like this: > {code:java} > <#macro myMacro arg1 arg2 arg3=3D"value3">... > <#assign myHash =3D {"arg1":"value1", "arg2":"value2"}> > <@myMacro **myHash/><#-- the hash contents are passed as parameters, inst= ead of the hash itself --> > {code} > This exists in Python: > {code:java} > def test_var_args_call(arg1, arg2, arg3): > pass > kwargs =3D {"arg3": 3, "arg2": "two"} > test_var_args_call(1, **kwargs) > {code} > Essentially the hash contents fill in any arguments not explicitly specif= ied. > For the case where arguments are specified in addition to the hash, you m= ay need to decide on a good syntax, e.g.: > {code:java} > <@myMacro arg1=3D"value1" **myHash/>{code} > This example doesn't have much precedent in freemarker syntax but is fair= ly understandable. > Although we don't need it nearly as much, the same could be done with lis= ts and function arguments: > {code:java} > <#function myFunc arg1 arg2>... > <#assign myList =3D ["val1", "val2"]> > ${myFunc(*myList)} > <#assign myList =3D ["val2"]> > ${myFunc("val1", *myList)} > {code} > Again similar to Python: > {code:java} > def test_var_args_call(arg1, arg2, arg3): > pass > args =3D ("two", 3) > test_var_args_call(1, *args) > {code} > You might want this for consistency, although in practice the hash expans= ion 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 int= o 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)