[stringtemplate-interest] dealing with empty list values
Terence Parr
parrt at cs.usfca.edu
Tue Feb 16 09:56:39 PST 2010
On Feb 8, 2010, at 7:11 PM, Joshua Royalty wrote:
>
>
> On Mon, Feb 8, 2010 at 1:51 PM, Terence Parr <parrt at cs.usfca.edu> wrote:
> 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?
>
> is null=false the intended way to do this? If so it.. works, but it doesn't seem like a clean solution. First you end up with lists with multiple types of values.. which is just generally not a good idea. Second.. it is nice that it preserves backwards compatibility, and its relatively easy to implement.. but it really seems like a solution tacked on at the last minute and its not intuitive at all to a programmer.
Well, the world was ok before I implemented that and it simply gives you a way to deal with missing values in a list. normally we want to ignore missing stuff. Rather than totally screw up the semantics of "missing" and iteration, this was a good solution.
> > 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.
>
> Again, the inconsistencies make it seem like this was tacked on instead of integrated into the framework.. And that makes it harder to remember and use.
Well, as I say, it hasn't really been an issue based upon the feedback I get.
> > 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()?
>
> I think I may have found the communications disconnect. I've been treating ':' as the 'list iterator' operator... you seem to be treating it as the 'apply template'/'expand list and apply template' operator..
Yes, I believe I call it "apply template" in the documentation. remember this is a functional language. there is no concept of for-loop. x:f() applies function f to x more or less
> If you think of ':' as the 'list iterator' operator (which is how the cheat sheet describes it..),
oops ;)
> then actually, no, blort should not be invoked because by using the 'list iterator' operator (instead of the 'invoke template' ($<template>(<attr>)$) operator) you are implying that it is a multi-valued attribute. (so $<attr>:<template>$ is a shortcut for $if(<attr>)$$<template>(<attr>)$$endif$ when <attr> is single valued... (though personally I would spell it out the second way..))
I tried to design ST so a single valued attribute and a multi-valued attribute only differ in cardinality. you can imagine an implementation that little used a list for everything including single valued attribute. the list notion is an artifact of implementation.
> I guess the real question it comes down to is what is more important.. convienience in the case that you want to not want to apply the template to null items in a list or having a consistent (and thereby intuitive) framework.
Well, you're the first person is brought this up ;)
> Needing to skip null elements in a list seems like a pretty uncommon case to me.. (I could be wrong), and its the only piece that would not be backwards compatible. If people really need to skip nulls, they could simply use 'strip'.. or there could be a global 'UseStripIterator' setting..
I would have to make a fundamental change in that x:f() would have to invoke f even when x it is null.
Ter
More information about the stringtemplate-interest
mailing list