You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Jeff Schnitzer <je...@infohazard.org> on 2012/03/01 07:44:15 UTC

[jexl] Test for existence

How can I test for the existence of something in jexl?

I've set lenient mode off in order to get more information - essentially
lenient mode suppresses any interesting errors.  But without lenient mode
there seems to be no way to test the presence or absence of a variable.
 I'm looking for something like:

if (foo?) { /* ok foo is a variable, might be null, but that's ok */ }

Is this possible?  It is common in template languages, I'm surprised it
isn't here.

Jeff

Re: [jexl] Test for existence

Posted by Jeff Schnitzer <je...@infohazard.org>.
Thanks, that is exactly what I was looking for!

I highly recommend separating "elvis operator" out as a separate line in
the docs from "ternary conditional".  My eyes blazed past it because my
mind said "I know what a ternary operator is and it's not what I'm looking
for".  I went up and down the page a couple times without seeing it.

Thanks again,
Jeff

On Fri, Mar 2, 2012 at 6:48 AM, henrib <he...@apache.org> wrote:

> Hi Jeff,
> Using the 'elvis' operator should allow what you seek; in strict mode, it
> allows non existent vars expressions to be handled without errors.
> Something
> like 'foo?:false' might do for you use case.
> Check http://commons.apache.org/jexl/reference/syntax.html on ternary
> conditional.
> Let us know if this fits your purpose,
> Cheers.
> Henrib
>
> --
> View this message in context:
> http://apache-commons.680414.n4.nabble.com/jexl-Test-for-existence-tp4434182p4438198.html
> Sent from the Commons - User mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>
>

Re: [jexl] Test for existence

Posted by sebb <se...@gmail.com>.
On 2 March 2012 11:48, henrib <he...@apache.org> wrote:
> Hi Jeff,
> Using the 'elvis' operator should allow what you seek; in strict mode, it
> allows non existent vars expressions to be handled without errors. Something
> like 'foo?:false' might do for you use case.
> Check http://commons.apache.org/jexl/reference/syntax.html on ternary
> conditional.

However the elvis operator does not distinguish undefined from null and false.

It might still be worth having a "defined" function as well?

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: [jexl] Test for existence

Posted by henrib <he...@apache.org>.
Hi Jeff,
Using the 'elvis' operator should allow what you seek; in strict mode, it
allows non existent vars expressions to be handled without errors. Something
like 'foo?:false' might do for you use case.
Check http://commons.apache.org/jexl/reference/syntax.html on ternary
conditional.
Let us know if this fits your purpose,
Cheers.
Henrib

--
View this message in context: http://apache-commons.680414.n4.nabble.com/jexl-Test-for-existence-tp4434182p4438198.html
Sent from the Commons - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org