[stringtemplate-interest] inconsistency with length function

Terence Parr parrt at cs.usfca.edu
Sat Jan 27 15:33:12 PST 2007


On Jan 16, 2007, at 9:51 PM, John Snyders wrote:

> The length function knows how to get the length of List, Map,  
> arrays, and Iterator. However convertAnythingIteratableToIterator  
> and convertAnythingToIterator are more general and get iterators  
> for Collection rather than List.
>
> I think that the length method in ASTExpr.java should be changed as  
> follows:
> From:
>
> } else if (attribute instanceof List) {
>   i = ((List)attribute).size();
> }
> To:
>
> } else if (attribute instanceof Collection) {
>   i = ((List)attribute).size();
> }

Howdy!  OK, yep, good fix.
> In addition I don't think Iterator should be handled like it is.  
> Getting the length of an iterator will use it up so that it can't  
> be used again.

yes, that's true, but how else to deal with iterators they pass in,  
for example?

> Some rethinking of how iterators are dealt with is needed. Until  
> then it would be good practice to never pass an iterator in as a ST  
> attribute.

That is very true.  They break the rules...they have side effects.

Ter


More information about the stringtemplate-interest mailing list