[stringtemplate-interest] ST new feature request: new expression option: test="..."

Zenaan Harkness zen at freedbms.net
Sun Apr 20 04:28:41 PDT 2008


Problem:
--------
I insert into my constructor template, a list of attributes.

Most attributes have a getter and a setter.

Some attributes are 'special', they only have getters;
in this case, the ctorFull must not include the atts with only getters,
since there is no local property for that attribute, only a getter
method (for example, a getter which is an alias for another getter, or a
getter which is a derived getter having custom code to derive its value
from other bean properties, or ... etc).

My ctorFull parameter list generator template currently looks like this:

   params(atts) ::= "(<atts:prop(); separator=\",\n\">)"

and where template "prop" sucks out the name of the property.

In this new variation, one of the properties has a derived getter, and
therefore an attribute of the property, hasSetter, returns false.

How do I exclude the property from the ctor?

Attempts:
---------

1) Anonymous subtemplate excludes property:
params(atts) ::= <<
(<atts:{<if(att.hasSetter)><prop()><endif>}; separator=",\n">)
>>

Problem: since we have ",\n" separator, the separator gets included even
for the excluded property, which clearly produces a syntax error in the
resultant code.

So, we cannot use 'separator' expression option.


2) As above, but without using builtin separator:
params(atts) ::= <<
(<first(atts):{<if(att.hasSetter)><prop()><endif>}>
<rest(atts):{<if(att.hasSetter)><\n><\t>,<prop()><endif>}>)
>>

Problem, since we're not using built-in separator, list processing must
be split into first and rest, and separator build into template. This
has the obvious problem of interaction with AutoIndentWriter, since the
separator is now at the start of every line.

Obvious solution, instead of first and rest, use front and last. Except,
as per emails from 25th Feb 2008 "Functional List Formatting", there is
no front (yet), so we must use more trickery in the form of a custom ST
subclass to provide for reversing lists (or build something into my
model, or ..??). Anyway, either way, this is seeming a bit inelegant,
and quite a bit of effort all up (fe, for those who follow in our
footsteps.


So,
3) With the proposed "test" expression option:

params(atts) ::= "(<atts:prop(); separator=\",\n\", test=\"it.hasSetter\">)"

Yes, now that's clean and elegant, intuitive and Just Works (TM)  :)

What do others think about such an expression option?

TIA
Zen

-- 
Homepage: www.SoulSound.net -- Free Australia: www.UPMART.org
Please respect the confidentiality of this email as sensibly warranted.


More information about the stringtemplate-interest mailing list