Re: Odd observation during debugging
Nathan Bubna <[email protected]>
| Newsgroups | gmane.comp.jakarta.velocity.user |
|---|---|
| Message-ID | <CAFyaDjG6S8hRUA6XkqXXNTTtUXTrDA6=fd=7ywvcFC6PUUdgHQ@mail.gmail.com> |
On Mon, Jul 16, 2012 at 9:04 AM, Christopher Schultz <[email protected]> wrote: > Nathan, > > On 7/11/12 1:35 PM, Nathan Bubna wrote: >> On Wed, Jul 11, 2012 at 9:58 AM, Christopher Schultz >> <[email protected]> wrote: >>> Nathan, >> ... >>>> See VELOCITY-731 and VELOCITY-692 and others... >>> >>> Those were good to read: 1.6 introduced more consistent (if slower) >>> treatment of references and then a setting was added to get the old >>> behavior. >>> >>> Any idea if a future version of Velocity (maybe 2.x) will have the >>> default value of that directive set to "false" so that toString will >>> only be called for users who actually want that? >>> >>> While backward-compatibility is certainly a reasonable goal, this >>> behavior kind of sucks... and I can't really think of too many cases >>> where it makes sense. Good that there is a setting for it ;) >> >> Actually, i still consider it correct for a template language (which >> is not the same as a scripting language) to treat render-as-empty as >> false in an #if. So, my hope for 2.0 was to not to remove that >> behavior, but to pre-empt it. > > On a somewhat related issue, we had been considering using "strict > reference mode" except that -- at least using the current if-directive > semantics, it's not practical to do things like this: > > #if($mymap && $mymap.containsKey('somekey') && > !$mymap.get('somekey').isEmpty() && ...) > > ...because many of those individual parts of the predicate may end up > returning something that is neither Boolean nor a Map, etc. and you > *must* test them for non-null-ness before trying to use them, otherwise > you risk an exception. > > Without disabling the 'toString' behavior in the configuration, there > will be no way for these types of checks to execute without spewing lots > of (in my case) useless String objects (not to mention the wasted time > to generate them in the first place). > > I've seen the use of things like this on occasion: > > #if($myobject == $null) > > My understanding was that a null reference would never be considered > equal to *anything*, even another null reference. Does the above > actually work? And the "$null" is just a reference that is assumed to be > null, right? When in strict-reference-mode, would that work since $null > has (presumably) never been defined? Yes, it works. And yes, $null is a reference assumed to be null. And i believe strict mode is concerned about key existence, so just do: context.put("null", null); And you should be able to use that.