[stringtemplate-interest] need a null value for arguments?

Udo Borkowski ub at abego-software.de
Tue Aug 2 00:33:14 PDT 2011


> Adding “undefined” seems pointless, since you can already get that by simply omitting an argument.


There are at least two situations I can think of that require an "undefined" literal, i.e. omitting an argument is not an option in those cases:

1. "Pass Thru"

Assume you have a template with many parameters and you are using pass thru ("…") to use the values from the caller. 

E.g.

caller(p1,p2,p3,p4,p5,p6,p7,p8,p9) ::= "      <t(…)>    "

t(p1,p2,p3,p4,p5,p6,p7,p8,p9="def9") ::= <<    <p9>    >>

If you want to call t but ensure p9 uses its default value and not the "pass thru" value from the caller you would write:

	<t(p9=undefined,…)>

Using null in this case would just omit the output for p9. 


2. "Argument by position"

If you are calling templates in the new "argument by position" style (i.e. omitting the parameter name in the arguments) the "undefined" comes in handy to get an "undefined" parameter in "middle" of the argument list.

E.g. you got this template 

t(p1="def1",p2="def2",p3="def3",p4="def4",p5="def5",p6="def6",p7="def7",p8="def8",p9="def9") ::= <<  ...    >>

and call it:

	<t("A",undefined,"C","D","E","F","G")>

Here p2 will get the value "def2".


> Due to the following expression, it seems reasonable to add a null constant with the value null:
> <t; null="stuff">


Good point. Agreed.


So it looks to me we should introduce both a null and an undefined literal. Both have their applications. 
* The null literal has the Java null as its value.
* The undefined literal has ST.EMPTY_ATTR as its value.


Udo


On 01.08.2011, at 22:58, Sam Harwell wrote:

> Adding “undefined” seems pointless, since you can already get that by simply omitting an argument.
>  
> Due to the following expression, it seems reasonable to add a null constant with the value null:
> <t; null="stuff">
>  
> Sam
>  
> From: stringtemplate-interest-bounces at antlr.org [mailto:stringtemplate-interest-bounces at antlr.org] On Behalf Of Udo Borkowski
> Sent: Monday, August 01, 2011 3:04 PM
> To: Terence Parr
> Cc: stringtemplate-interest List
> Subject: Re: [stringtemplate-interest] need a null value for arguments?
>  
> Also: to avoid confusion with the Java null maybe call the constant 'undefined'?
> 
> I was kind of thinking that it should be null because it has the same semantics as passing all in to add().
>  
> Actually this is not the case as you can see in the following test case. That's why I thought mixing "null" und "undefined" isn't such a good idea.
>  
> BTW: in the implementation the "undefined" literal should probably get the value ST.EMPTY_ATTR  rather than null.
>  
>  
> public class UndefinedOrNullTest extends BaseTest {
>        @Test
>        public void testUndefinedParameter() throws Exception {
>               writeFile(tmpdir, "t.stg", "t(s=\"world\") ::= <<\nHello <s>\n>>");
>               String path = tmpdir + "t.stg";
>  
>               STGroup group = new STGroupFile(tmpdir + "/t.stg");
>               ST st = group.getInstanceOf("t");
>               String s = st.render();
>               Assert.assertEquals("Hello world", s);
>        }
>       
>        @Test
>        public void testNullParameter() throws Exception {
>               writeFile(tmpdir, "t.stg", "t(s=\"world\") ::= <<\nHello <s>\n>>");
>               String path = tmpdir + "t.stg";
>  
>               STGroup group = new STGroupFile(tmpdir + "/t.stg");
>               ST st = group.getInstanceOf("t");
>               st.add("s", null);
>               String s = st.render();
>               Assert.assertEquals("Hello ", s);
>        }
> }
>  
>  
> Udo
>  
> On 01.08.2011, at 21:33, Terence Parr wrote:
> 
> 
> 
> On Aug 1, 2011, at 7:10 AM, Udo Borkowski wrote:
> 
> 
> Not sure about the semantic of null.
>  
> E.g. assume you have this template
>  
> t(s="world") ::= "Hello <s>"
>  
> What will
>  
> <t(null)>
>  
> render?
>  
> a) "Hello " (i.e. same as <t("")>)
> b) "Hello world" (i.e. same as <t()>)
> 
> I would assume it would render this one since the parameters missing.
> 
> 
> c) "Hello null"
> d) something else?
>  
>  
> Also: to avoid confusion with the Java null maybe call the constant 'undefined'?
> 
> I was kind of thinking that it should be null because it has the same semantics as passing all in to add().
> 
> Ter
> _______________________________________________
> stringtemplate-interest mailing list
> stringtemplate-interest at antlr.org
> http://www.antlr.org/mailman/listinfo/stringtemplate-interest
>  

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/stringtemplate-interest/attachments/20110802/0b764297/attachment-0001.html 


More information about the stringtemplate-interest mailing list