You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Will Glass-Husain <wg...@forio.com> on 2002/06/01 17:33:36 UTC

Generalizing equals (was Character == String)

Hi,

Just to make a bit of a fuss, why not make "==" more general purpose?

I suggest that '==' do two things
(1) If the types are the same, check for equality with .equals
(2) If the type are not the same, convert both sides to string, then check for 
equality again

While not perfect, this would help solve two problems I've encountered:

(1) When template designers have an integer or double in the context but use a 
quoted literal as a comparison
   $AnInteger == 1
   $AnInteger == "1"
   $ADouble == "1.0"
   $ADouble == 1.0

(this would not solve the double issue completely of course, due to number 
format issues).

(2) When I have a Torque object in Velocity, the key fields are of type 
NumberKey (based on BigDecimal), which always returns false when comparing with 
an integer

   ## Are we dealing with a known record
   $DatabaseObject.Key == 1

As I've noted before, the concept of type is difficult for template designers 
to understand, particularly in a language like Velocity that appears to have no 
type (but really does).

This change is mostly backward compatible, meaning that only those template 
designers who relied upon types being different in an equality test would 
notice the change.  (I think this is unlikely).

The counter argument (I know exactly who will weigh in with this point) is that 
with good MVC practice template designers shouldn't be comparing references 
with literals.  But people do, so why not make it work?

Best, WILL

P.S.  If this seems reasonable, I'd be happy to submit a patch.


Quoting Jeff Schnitzer <je...@infohazard.org>:

> I have some data that comes into the template as Character, in fact
> 'M'
> and 'F'.  I would like to be able compare to the value of it with ==,
> but that doesn't appear possible.
> 
> I propose that Character should be converted to string for ==.  That
> way
> this works:  #if($foo.theCharacter == "M")
> 
> I'm currently using a tool for this, but it seems appropriate for
> Velocity to handle it.
> 
> I'm really new to the Velocity code, but it looks like this could be
> implemented by adding a special-case in ASTEQNode:
> 
> +       if (!(left instanceof Character && right instanceof
> Character))
> +       {
> +           if (left instanceof Character)
> +               left = left.toString();
> +
> +           if (right instanceof Character)
> +               right = right.toString();
> +       }
> +
>         if (left.getClass().equals( right.getClass() ) )
>         {
>             return left.equals( right );
>         }
> 
> Alternatively, maybe Character should always be converted to String
> everywhere inside Velocity.  Why have extra types?
> 
> Jeff Schnitzer
> jeff@infohazard.org
> 
> --
> To unsubscribe, e-mail:  
> <ma...@jakarta.apache.org>
> For additional commands, e-mail:
> <ma...@jakarta.apache.org>
> 
> 



------------------------------------
Will Glass-Husain  <wg...@forio.com>
Forio Business Simulations
office: (415) 440-7500
mobile: (415) 235-4293

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>