Re: Hashes (in general)

Daniel Dekany <[email protected]>
Newsgroups gmane.comp.web.freemarker.user
Message-ID <[email protected]>
Monday, June 8, 2009, 10:57:45 PM, Brian Pontarelli wrote:

> I've been using FM for a while now and I just ran into a interesting  
> snag. I knew that Hashes define in the FTL files had to have a String
> for the key. This seems like it is to support the 'foo.bar' syntax  
> (which I'm a huge advocate against). However, I'm using JCatapult,  
> which takes properties and fields of an action class and puts them  
> into the root FM context. I have this:
>
> public class Foo {
>    public Map<Integer, String> guests;
>    public Map<Integer, String> miles;
>
>    public void setupForm() {
>      guests.put(1, "1");
>      ...
>    }
> }
>
> This Map is then being passed to a custom Directive as an attribute  
> like this:
>
> [@jc.select items=guests name="q.guests"/]
>
> There are a few issues I'm running into.
>
> 1. FM seems to wrap this Map and rather than converting the keys to  
> Strings during wrapping, it keeps the original map around and seems to
> incorrectly duplicate the keys with String versions whose values are  
> all null.
>
> 2. I get random ConcurrentModificationExceptions out of FM if I use an
> iterator like this:
>
>     for (Map.Entry e : map) {
>       ...
>     }
>
> So, my questions are:
>
> - Will 2.4 or some other version will support Maps with any type of  
> key so that frameworks can use Object comparisons (via the equals  
> method)?

With the proper wrapper you can access the Java Map facility (i.e.,
you can use non-string keys; see Attila's answer), but the FTL hash
type is defined as something that associates *strings* with arbitrary
values. So it's not the same as Java's Map-s. For that reason
myMap[key] will always expect a string in FTL. Anyway, I think that a
template language should rather have something like Java's Map-s than
something like FTL's hashes... but in 2.4 the hash type still only
allow string keys.

> - Is there a way currently to get FM to stop wrapping the Map with an
> incorrect version that has String keys and null values?

Does such thing happen? Maybe it's just that for missing items the
Hash API (also the Java Map API) returns Null. So, myMap["1"] is null,
because indeed there is no item with key "1" (only with key 1).

> Thanks,
> -bp

-- 
Best regards,
 Daniel Dekany


------------------------------------------------------------------------------
Crystal Reports - New Free Runtime and 30 Day Trial
Check out the new simplified licensing option that enables unlimited
royalty-free distribution of the report engine for externally facing 
server and web deployment.
http://p.sf.net/sfu/businessobjects
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.