[stringtemplate-interest] dealing with empty list values

Terence Parr parrt at cs.usfca.edu
Mon Feb 8 13:51:20 PST 2010


On Feb 7, 2010, at 5:02 PM, Joshua Royalty wrote:
Hi Terence,
> 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'.

unless you use the null option, right?

>  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'.

Well, if foo is missing then $foo:blort()$ should not evaluate blort() and it doesn't. Therefore when foo his multi-valued, it should skip any empty values to be consistent, right?

> 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)).  

You're right.  that is inconsistent. on the other hand, given that you might be using the null option, there has to be a way to figure out how many total elements you're going to have.

> 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.

I understand what you're saying, but what about the case when foo a single value that I have above? should it invoke blort()?

> 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.  

using null in this way is its intended function, so doesn't really seem like a hack for my perspective ;)

Ter


More information about the stringtemplate-interest mailing list