[ 
https://issues.apache.org/jira/browse/VELOCITY-692?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12975681#action_12975681
 ] 

Nathan Bubna commented on VELOCITY-692:
---------------------------------------

No, the order should be about frequency of types, not closeness in meaning to 
each other.  And for that very reason, i've revamped the order (see below).  As 
for 0, i've struggled with that because i know many people might expect 0 to be 
treated as it is in javascript.  However, this is not a scripting language but 
a display language, and 0 is often a very important thing to display.  My 
inclination is to leave it as non-empty for now.  Besides, checking to see if a 
number is zero takes more processing than you'd think.  Anyway, here's the 
order i am implementing for now...

#if( $obj ) will be false if:

$obj is null
$obj is boolean false
$obj returns false from getAsBoolean()
$obj is empty string (CharSequence w/length 0)
$obj returns true from isEmpty()
$obj is array of length 0
$obj returns null from getAsString()
$obj returns empty string from getAsString()
$obj returns null from getAsNumber()
$obj returns 0 from length() or size()
$obj returns empty string from toString()

Type-wise, this prioritizes those most likely to be used in #if( $obj) (by my 
completely subjective experience), as such:  booleans, strings, 
maps/collections, and arrays.  then it gives getAsString(), getAsNumber(), 
length() and size() all opportunities, falling back on toString() as the last 
resort.   So, if you have a custom object likely to end up alone in an #if 
directive, you'll get the best performance by implementing a getAsBoolean() 
method, or at least, isEmpty().

Also, i might add that organizing things this way and considering the various 
types likely to end up in #if, the length()/size() testing feels a bit 
unnecessary.   What do you guys think?

> have #if handle empty strings/arrays/collections/maps more conveniently
> -----------------------------------------------------------------------
>
>                 Key: VELOCITY-692
>                 URL: https://issues.apache.org/jira/browse/VELOCITY-692
>             Project: Velocity
>          Issue Type: New Feature
>          Components: Engine
>            Reporter: Nathan Bubna
>            Priority: Trivial
>
> An idea from the dev list:
> -------------------------------------------------------------------------------------------------
> On Sat, Feb 7, 2009 at 3:41 PM,  <[email protected]> wrote:
> > Hello,
> > I wanted to share with you a few ideas I have about new simple
> > improvements for DisplayTools. I should be able to make patches for
> > them if you are interested.
> >
> > 1. Add new method
> >
> > isEmpty(object)
> >
> > that will return true if the object is null or empty (for strings it's
> > zero length; for collections, maps and arrays it's zero size). This
> > should help with  annoying null checks. (Probably a better place for
> > this method would be Engine, not Tools)
> yeah, not something for tools.  would be interesting to have the
> Uberspect pretend that every non-null reference has an isEmpty()
> method, or perhaps just add 0-length strings, empty collections, empty
> maps and 0-length arrays to the list of things that #if( $foo )
> considers false.
> -------------------------------------------------------------------------------------------------

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to