Re: String comparisons
Daniel Dekany <[email protected]>
| Newsgroups | gmane.comp.web.freemarker.devel |
|---|---|
| Message-ID | <[email protected]> |
Wednesday, January 30, 2013, 10:43:30 PM, Jaime Garza wrote:
> In the code on ComparisonExpression:
>
> else if(ltm instanceof TemplateScalarModel && rtm instanceof TemplateScalarModel) {
> if(operation != EQUALS && operation != NOT_EQUALS) {
> throw new TemplateException("Can not use operator "
> + opString + " on string values.", env);
> }
> String first =
> EvaluationUtil.getString((TemplateScalarModel)ltm, left, env);
> String second =
> EvaluationUtil.getString((TemplateScalarModel)rtm, right, env);
> comp = env.getCollator().compare(first, second);
> }
>
> Collator supports "less", "less than", "greater" and "greater than" semantics.
>
> What is the caveat on not allowing anything but equals and not equals? There must be a reason.
The collators of the current locale(!) could be used technically (but
with what settings?). But it's hard to find a reason to compare two
strings in a template for anything but equality, so when FreeMarker
runs into another kind of comparison, it's very likely that it's just
the result of bug in the application. Typically, the user meant to
compare two numbers or dates which come from XML or as HTTP request
parameters, but have forgotten to parse them to number or date. A
string comparison would silently give the wrong answer. It's
dangerous, especially as there are cases where it accidentally gives
the good answer, and that will happen while you are testing, of
course... So it's better to throw an exception.
> (Having all the semantic behaviors would be a great way to deal
> with date comparisons when I need to convert dates to text, and back to a date.)
You want to compare dates in string form? I guess that happens to work
with ISO dates... but not with most date formats.
> Thanks
> Jaime
--
Best regards,
Daniel Dekany
------------------------------------------------------------------------------
Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
Download AppDynamics Lite for free today:
http://p.sf.net/sfu/appdyn_d2d_jan