[
https://issues.apache.org/jira/browse/VELOCITY-692?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12975337#action_12975337
]
Nathan Bubna commented on VELOCITY-692:
---------------------------------------
So, i'm still of the opinion that having "empty" values be treated as false by
#if( $foo ) is a good idea. so i thought i'd restart this conversation by
reminding people that VTL is a display language; it is not meant to be a full
scripting language. so, while an empty array or empty map may not be "falsey"
to a script, the lack of anything in the array or map to display makes it quite
falsey to a display language, no?
So, to recap, i propose that we change #if in 2.0 to treat the following values
as false:
$obj that is null
$obj that is boolean false
$obj that returns false from a getAsBoolean method
$obj that is empty string ""
$obj that is empty array
$obj that returns true from an isEmpty() method (i.e. Collection and Map
implementations)
$obj that returns null or empty string from a getAsString method
$obj that returns 0 from a length() or size() method
$obj that returns empty string from toString() method
i also propose that the order above be the order in which things are checked.
This means that while we will still check toString(), it will not ever be
called on any maps or lists or any object that bothers to implement a
getAsBoolean, getAsString or isEmpty() method. Again to be more clear, if the
object has a getAsBoolean(), isEmpty(), getAsString(), length(), or size()
method, then we don't keep looking further. Once we find one of these methods,
we return false if the result is falsey or true otherwise. This makes it easy
for users to avoid expensive calls to toString() or even getAsString().
For those who want to distinguish between false and falsey or null and empty,
they will have to use #if( $obj == $null ) or #if( $obj == false ) to be
explicit about what they want to know. So, there is no loss of functionality
involved here. It is merely a change to make empty values be treated as false
by #if( $foo ) calls and simultaneously make it easy to avoid expensive
toString() calls when using #if to test references.
Thoughts, tirades, minor objections?
p.s. the getAsString and getAsBoolean support will probably be checked in this
evening.
> 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]