Re: variable?? is not checking for null ... please help

Daniel Dekany <[email protected]>
Newsgroups gmane.comp.web.freemarker.user
Message-ID <[email protected]>
Tuesday, August 7, 2012, 12:55:19 AM, marcusadamski wrote:

> Hi all;
>
> A little bit of a newbie with Freemarker, but according to the documentation
> the following should work:
>
> <#assign result = mypackage.function()!>
>    <#if result??>

The problem is that with the `!` you said that if the return value is
null, then it should be defaulted to empty string + empty sequence +
empty hash (a multi-type value). So after that `result` does exist,
but it will not have a `function` sub-variable.

Of course, I see why the `!` is there. FreeMarker refuses to do
*anything* with null, other than applying `!` and `??` and like on it,
so you can't even store null. Certainly you don't want to call the
function twice either (first to check if it's null, then to store the
non-null result). It's an annoying heritage. The hack to work this
around is using a default that can't be returned by the function. For
example, assuming that you know that the function can't return an
empty Map or empty Collection or empty String:

<#assign result = mypackage.function()!>
  <#if result?has_content>

-- 
Best regards,
 Daniel Dekany


------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.