[stringtemplate-interest] "rest" and template recursion

Sasha Crosland sasha.crosland at gmail.com
Fri May 30 20:22:46 PDT 2008

Hi everyone,

I had been having difficulty getting template recursion to work as I had
hoped it would, and when I searched briefly for anything addressing this
topic in the list archives, I found that others had apparently had a similar
problem to mine.  From what I gathered, the problem is not to do with
recursion itself, but with use of the "rest" operator on a multi-valued
attribute, which I have been trying to use in the implementation of my
recursive template.  Something to do with "rest" returning an iterator
instead of a list?

What I want to do is to generate the names of Java types such as "Fn<P1,
Fn<P2, Fn<P3, R>>>," which I am using as a "curried" representation of a
three-argument function with type P1 x P2 x P3 -> R.  I tried to define a
template called curriedFnType taking a single attribute called arity, which
is a list of integers such that arity[i] = i for i from 1 to length(arity).

curriedFnType(arity) ::= <<
Fn\<P<first(arity)>, <curriedFnType(arity=rest(arity))>\>

The problem is that for an arity of four ([1,2,3,4]), for example, the
template is instantiated to "Fn<P1, Fn<P2, Fn<P4, R>>>", which somehow skips
over P3.  I encounter similar quirkiness with whatever sort of recursive
template I try to implement in this manner.  And I don't think that using
the colon operator to apply a template to the multi-valued arity attribute
will work directly, will it?, since we aren't dealing with tail-recursion.
The alternative

curriedFnType2(arity) ::= "<arity:{Fn\<P<it>, }>R<arity:{\>}>

seems to work, but, to be honest, it feels rather lame that I should have to
resort to this.  Can anybody shed any light on the issue?  Is this indeed a
bug, or am I overlooking something?  I am using StringTemplate 3.1.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org:8080/pipermail/stringtemplate-interest/attachments/20080530/8921f976/attachment.html 

More information about the stringtemplate-interest mailing list