Kallin Nagelberg | 16 Jul 22:45

[groovy-user] referencing a binding variables that's not passed

I have a groovy script that may or may not be called from another script.

In the the callee I'm trying to do something like:

if (bindingvar){
// do something
}

It works great when bindingvar is in the binding (ie the other script called it), but when I run the script alone it complains of nosuchproperty.
How can I check to see if the property is passed without catching that exception?
Jim White | 16 Jul 23:20

Re: [groovy-user] referencing a binding variables that's not passed

Kallin Nagelberg wrote:

> I have a groovy script that may or may not be called from another script.
> 
> In the the callee I'm trying to do something like:
> 
> if (bindingvar){
> // do something
> }
> 
> It works great when bindingvar is in the binding (ie the other script 
> called it), but when I run the script alone it complains of nosuchproperty.
> How can I check to see if the property is passed without catching that 
> exception?

if (binding.variables.containsKey("bindingVar")) {
// do something
}

Or if you'd like to get a null value for an optional binding:

def optVar = binding.variables.get("bindingVar")
if (optVar) {
// do something
}

This works because your script gets compiled as a subclass of 
groovy.lang.Script and it has a getBinding() method (along with other 
useful ones like println, run, and evaluate).

Jim

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

Kallin Nagelberg | 16 Jul 23:26

Re: [groovy-user] referencing a binding variables that's not passed

Thanks Jim, you're a life saver !

On Wed, Jul 16, 2008 at 5:20 PM, Jim White <jim-3x83HEs+8FyL7B8feK0ILA@public.gmane.org> wrote:
Kallin Nagelberg wrote:

I have a groovy script that may or may not be called from another script.

In the the callee I'm trying to do something like:

if (bindingvar){
// do something
}

It works great when bindingvar is in the binding (ie the other script called it), but when I run the script alone it complains of nosuchproperty.
How can I check to see if the property is passed without catching that exception?

if (binding.variables.containsKey("bindingVar")) {
// do something
}

Or if you'd like to get a null value for an optional binding:

def optVar = binding.variables.get("bindingVar")
if (optVar) {
// do something
}

This works because your script gets compiled as a subclass of groovy.lang.Script and it has a getBinding() method (along with other useful ones like println, run, and evaluate).

Jim


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

  http://xircles.codehaus.org/manage_email



Dierk König | 17 Jul 08:22

RE: [groovy-user] referencing a binding variables that's not passed


 if (binding.variables.bindingvar) { ... }

cheers
Dierk

| -----Original Message-----
| From: Kallin Nagelberg [mailto:kallin.nagelberg@...] 
| Sent: Mittwoch, 16. Juli 2008 22:48
| To: user@...
| Subject: [groovy-user] referencing a binding variables that's 
| not passed
| 
| I have a groovy script that may or may not be called from 
| another script.
| 
| In the the callee I'm trying to do something like:
| 
| if (bindingvar){
| // do something
| }
| 
| It works great when bindingvar is in the binding (ie the 
| other script called it), but when I run the script alone it 
| complains of nosuchproperty. 
| How can I check to see if the property is passed without 
| catching that exception?
| 
| 

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

| 17 Jul 10:04

Re: [groovy-user] referencing a binding variables that's not passed



2008/7/17 Dierk König <dierk.koenig-vS5zOjhOW+wAvxtiuMwx3w@public.gmane.org>:

 if (binding.variables.bindingvar) { ... }

Only if the bindingvar never contains false, 0, an empty String, an empty Collection..., of course.

- Jörg

-
A Groovy user
Jochen Theodorou | 17 Jul 11:38

Re: [groovy-user] referencing a binding variables that's not passed

Dierk König schrieb:
>  if (binding.variables.bindingvar) { ... }

isn't that wrong? What if the variable is there, but evaluates to false?

bye blackdrag

--

-- 
Jochen "blackdrag" Theodorou
The Groovy Project Tech Lead (http://groovy.codehaus.org)
http://blackdragsview.blogspot.com/
http://www.g2one.com/

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

Jim White | 17 Jul 14:43

Re: [groovy-user] referencing a binding variables that's not passed

Jochen Theodorou wrote:

> Dierk König schrieb:
> 
>>  if (binding.variables.bindingvar) { ... }
> 
> isn't that wrong? What if the variable is there, but evaluates to false?

It's just an idiom that is likely to be useful for the original poster's 
question because the logic of an application that deals with an optional 
parameter will typically do the same thing if it is false or not set, or 
in other words the optional path is only taken iff the optional 
parameter is set and the value is not false.

The real value of the solution is that it doesn't throw an exception.

If you strictly want to know if the variable exists at all then you must 
use containsKey.

And I do prefer the property accessor form (rather than using get), 
which I think is the one I use in my code but hadn't gone to look for it 
when I made my first post.

Of course either one is better than array access form because I think it 
is a travesty that missing indicies don't get an array index OOB 
exception.  In fact it is the inconsistency with Groovy's normal 
property access (Binding throws a missing variable exception for missing 
keys) that makes this workaround necessary.

Jim

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

Jochen Theodorou | 17 Jul 14:49

Re: [groovy-user] referencing a binding variables that's not passed

Jim White schrieb:
> ty that missing indicies don't get an array index OOB 
> exception.  In fact it is the inconsistency with Groovy's normal 
> property access (Binding throws a missing variable exception for missing 
> keys) that makes this workaround necessary.

inconsistency or not depends on your view. Let's say from a PHP view it 
might not make much sense, but if you think about accessing a variable 
that is not yet defined, then it makes sense to throw an exception, 
because in almost all cases it is an error.

bye blackdrag

--

-- 
Jochen "blackdrag" Theodorou
The Groovy Project Tech Lead (http://groovy.codehaus.org)
http://blackdragsview.blogspot.com/
http://www.g2one.com/

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

Jim White | 17 Jul 16:37

Re: [groovy-user] referencing a binding variables that's not passed

Jochen Theodorou wrote:

> Jim White schrieb:
> 
>> ty that missing indicies don't get an array index OOB exception.  In 
>> fact it is the inconsistency with Groovy's normal property access 
>> (Binding throws a missing variable exception for missing keys) that 
>> makes this workaround necessary.
> 
> 
> inconsistency or not depends on your view. Let's say from a PHP view it 
> might not make much sense, but if you think about accessing a variable 
> that is not yet defined, then it makes sense to throw an exception, 
> because in almost all cases it is an error.

Which is also the case with array inidices.  Hence the inconsistency.

Jim

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

Jochen Theodorou | 17 Jul 17:06

Re: [groovy-user] referencing a binding variables that's not passed

Jim White schrieb:
> Jochen Theodorou wrote:
> 
>> Jim White schrieb:
>>
>>> ty that missing indicies don't get an array index OOB exception.  In 
>>> fact it is the inconsistency with Groovy's normal property access 
>>> (Binding throws a missing variable exception for missing keys) that 
>>> makes this workaround necessary.
>>
>>
>> inconsistency or not depends on your view. Let's say from a PHP view 
>> it might not make much sense, but if you think about accessing a 
>> variable that is not yet defined, then it makes sense to throw an 
>> exception, because in almost all cases it is an error.
> 
> Which is also the case with array inidices.  Hence the inconsistency.

when you access an array index that does not exist, you get an 
exception... so what is the inconsistency? Of course in a language that 
allows you to access variables that do not exist yet, it makes sense to 
allow the access to an array index that does not access too... for 
example why should "print a" succeed even though a does not exist, but 
"print a[1]" fail if a does not exist?

bye blackdrag

--

-- 
Jochen "blackdrag" Theodorou
The Groovy Project Tech Lead (http://groovy.codehaus.org)
http://blackdragsview.blogspot.com/
http://www.g2one.com/

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Gmane