[stringtemplate-interest] [ST4] How to avoid map key iteration?
Udo Borkowski
ub at abego-software.de
Sun Feb 13 01:09:16 PST 2011
Hi Ter,
> Hi. my first thought is, don't try to iterate over the object.
> ...
> <adr:{t|<t.zip> <t.city>}>
:-)
OK, I was trying to make the example as simple as possible, just to show my case. Of cause this simple example could be written as you suggest.
My point is: when writing:
<adr:{t|<t.zip> <t.city>}>
I don't want to "iterate" over the object but rather pass an argument to the anonymous templates. The "iterate" happens accidently, just because adr also is iterable.
Here the more complete example:
Assume we have this template:
t1(adr) ::= <<
<adr.firstname> <adr.lastname>
<adr.zip> <adr.city>
>>
With my "bean style" model object I get a result like this:
---------
Joe Smith
12345 Somecity
---------
Most times the format "zip city" is OK. However sometimes it needs to be formatted differently.Therefore I make the second line a parameter with the common case as the default:
t2(adr,line2={<adr:{a|<a.zip> <a.city>}>}) ::= <<
<adr.firstname> <adr.lastname>
<line2>
>>
This way I can still call the template like this most of the time:
<t2(adr=p)>
and when a different 2nd line format is needed, I pass it in:
<t2(adr=p,line2={<adr:{a|<a.city>, <a.zip>}>})>
In this example the result would be:
----------
Joe Smith
Somecity, 12345
----------
This all works fine with the bean-style model object.
However when I switch to a model object that extends a Map class things break and I get an error like:
no such property or can't access: java.lang.String.zip
I hope this somewhat lengthy example make things more clearer.
I guess from a language design point of view I would provide different constructs for the "iterate" vs. "pass argument to anonymous template" case, e.g.
<adr:{…}> // the "iterate" case, i.e. "anything iterable" is used as an iterator, as now
<adr::{…}> // the "pass arg" case, i.e. don't handle iterables differently, just pass in to anon template
Udo
On 11.02.2011, at 17:39, Terence Parr wrote:
> Hi. my first thought is, don't try to iterate over the object.
>
> <adr.zip> <adr.city>
>
> is same as:
>
> <adr:{t|<t.zip> <t.city>}>
>
> Ter
> On Feb 11, 2011, at 5:45 AM, Udo Borkowski wrote:
>
>> Hi,
>>
>> is there any way to prevent the "iteration" over map keys when using a template like <m:{t|…}> (m may be a map or a simple object)?
>>
>> Details:
>>
>> I am using a template like this:
>>
>> writeAddress(adr) ::= <<
>> ...
>> <adr:{t|<t.zip> <t.city>}>
>> ...
>> >>
>>
>> adr represents an "Address" with properties "street", "zip", "city" etc.
>>
>> Initially this was implemented by a bean-style model class Address. Everything worked as expected.
>>
>> Now I changed the model class to a more "dynamic" implementation by using a Map. I.e. "zip", "city", etc. are now keys in the map, mapping to their resp. values.
>>
>> Expressions like this:
>>
>> <a.zip>
>>
>> still work fine, but
>>
>> <adr:{t|<t.zip> <t.city>}>
>>
>> runs into an error:
>>
>> no such property or can't access: java.lang.String.zip
>>
>> This is because the new model instance "accidentally" is a Map and the expression now iterates over the keys of the map ("zip" being the first).
>>
>> How can this iteration be avoided?
>>
>>
>> Udo
>>
>>
>> _______________________________________________
>> stringtemplate-interest mailing list
>> stringtemplate-interest at antlr.org
>> http://www.antlr.org/mailman/listinfo/stringtemplate-interest
>
> _______________________________________________
> stringtemplate-interest mailing list
> stringtemplate-interest at antlr.org
> http://www.antlr.org/mailman/listinfo/stringtemplate-interest
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/stringtemplate-interest/attachments/20110213/d0c92634/attachment.html
More information about the stringtemplate-interest
mailing list