[stringtemplate-interest] dealing with empty list values

Joshua Royalty joshua.royalty at gmail.com
Sun Feb 7 17:02:26 PST 2010


Hi Terence,

You're right, I was mistaken to say that it treated them as the empty string
and thus I worded things badly.  The main logical inconsistency I am seeing
is less in how the value is treated than in how the iterator works as
compared to iterators in other languages.

If $foo$ is 'missing', then
'<tr>$if(foo)<b>$foo$</b>$else$MISSING$endif$</tr>' will still print
'<tr>MISSING</tr>', but $values:{ v |
<tr>$if(v)<b>$v$</b>$else$MISSING$endif$</tr>}$ will not print
'<tr>MISSING</tr>' if one of the values is 'missing'.  Obviously in the
first case ST does not know what text it should skip and the user obviously
intended it to print <tr>MISSING</tr>, and I contend that ST also cannot
know if it should skip in the second case and that the user obviously
intended it to print <tr>MISSING</tr> for values that were 'missing'.  When
I use a list iterator I intuitively expect it to iterate over every value in
the list, it should be the same as repeating the code inside the template
for every value in the list, even if that value is 'missing'.

Also, length(values) return the length of the list with the nulls included,
and in order to get the length without them you need to call
length(strip(values)).  To be consistent with the current ST list iterator
length(values) should automatically strip nulls...  However, another fix
would be to not automatically skip nulls in the list iterator.  This would
fix both of the inconsistencies and be much more intuitive.  If people want
to skip the nulls entirely when iterating they can use strip as a shortcut,
$strip(values):{ v | <tr><b>$v$</b></tr>}$.  For backwards compatibility /
convienience you can add a global 'AutoStripLists' option if you think its
necessary.

Given that the thread is from 2006 I think it may have been before the 'if'
statement was added.  As such the null option would have been a perfect fix
since this problem only exists when the if statement is used..  I did find a
hack using the null option that works ($values:{ v |
<tr>$if(v)<b>$v$</b>$else$MISSING$endif$</tr>}; null=false$), but I really
don't like using it and it doesn't fix the inconsistency issues.

-Josh

PS: I also decided to try '$values:{ v | $if(v)$$v$$else$-1$endif$};
separator=\", \", null=null$' and that makes it not skip the null values
(which it really shouldn't...).  However, it also prints 'test1, , test2'
instead of 'test1, -1, test2'...  I then tried null=blah and that did the
same thing so I'm pretty sure this should be a syntax error if null=foo
where foo is not a string and is not true/false


On Sun, Feb 7, 2010 at 12:01 PM, Terence Parr <parrt at cs.usfca.edu> wrote:

> Hi Joshua, I wrestled with this concept for quite a while about null values
> in a list. First, let me remind you that a missing parameter $foo$ is not
> the empty string--it's actually missing. to be consistent, I decided that
> null values in a list are missing not empty or blank.
>
> So, my solution to that posting you mentioned is to allow the null option.
>
> Ter
> On Feb 6, 2010, at 2:52 AM, Joshua Royalty wrote:
>
> > Hi, I ran into a bit of a stumbling block with a list containing null
> values.  It seems that ST automatically skips over null values when
> iterating over a list.
> >
> > I searched the archives and found this thread
> >
> http://markmail.org/search/?q=list%3Astringtemplate+dealing+with+empty+list+values#query:list%3Astringtemplate%20dealing%20with%20empty%20list%20values+page:1+mid:fvkxdehqyuyu4jxp+state:results
> >
> > Was a solution ever implemented?
> >
> > Personally I would find it most intuitive to have the default be to not
> skip nulls.  Such that "$values:{ v | $if(v)$$v$$else$-1$endif$" would be
> identical to "$values; separator=\", \", null=\"-1\"$"
> >
> > Instead, if you sent values to {test1, null, test2} you get
> > test1, test2
> > test1, -1, test2
> >
> > "$values:{ v | $if(v)$$v$$else$-1$endif$}; separator=\", \",
> null=\"-1\"$" also prints 'test1, -1, test2'
> >
> > Its not logically consistent to skip nulls when iterating over a list and
> treat them as "" (Empty String) everywhere else.  (A case could also be made
> that it is not logically consistent to treat them as false in the if
> statement as well)
> >
> > If someone wants to clean nulls out of the list they can use the strip()
> function, though strictly speaking that should have been handled by the
> model.
> >
> >
> > _______________________________________________
> > 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/20100207/7bd9c045/attachment.html 


More information about the stringtemplate-interest mailing list