From: Chris Greener <chris.greener@ma...> - 2003-02-19 01:25:52
My personal bias is var as it's short and consistent with languages such as
I'm assuming the same will apply to other constructs e.g.
From: Pat [mailto:pat@...]
Sent: Thursday, February 13, 2003 1:26 PM
Subject: [Beanshell-users] Proposal - Variable scoping change for BeanShell
[ I'd really appreciate any comments on the following proposal ]
After a long while of wrestling with the issue, I'm beginning to think that
we should bite the bullet and make a change to BeanShell's variable scoping
to make it look more like Java. (If you already know about the issue, skip
to '*** the proposal ***' below...)
The ambiguity comes about because BeanShell allows you to use undeclared
variables and on assignment these variables default to local scope. That is
when you say:
x = 5;
it assumes you are creating a new variable 'x' in the local scope, even if
there was an 'x' declared in your parent's scope. e.g.
int x = 1;
print( x ); // 1!
The above shows the ambiguity. One might expect that after the call to
bar() the value of 'x' would have changed to 5, but by default in BeanShell
it does not.
When strict Java mode is enabled (and with appropriate fixes in the above
script) the behavior is different: undeclared variable assignments will then
always look in the parent namespace first. Therefore in strict java mode x
would be 5.
BeanShell has always offered scope modifiers (this.x, super.x, global.x) to
deal with this situation. But these are very unsatisfying to use.
Another trick I often use is to put all of my variables into an object of
some type, so the reference is always unabiguous:
However this is also not very satisfying.
*** the proposal ***
I'm therefore proposing that in 1.3 we change the default behavior to behave
for purposes of variable scoping as the strict java mode does and to offer
the old mode for legacy code through some switch or configurable parameter.
The issues that this raises of course are -
1) How much code would this break?
2) What would one do to use a local untyped variable, should one want to?
As for #1 - I don't know. But my guess is not much and that the change will
be easy enough to understand and test for that it's worth it.
As for #2 - Of course the easiest thing would be to simply declare the
variable with a type, e.g. 'int x = 5;' instead of 'x=5;' to force it
to local scope (as in Java).
However as a purist I would still like a way to say that a variable is a
but untyped variable. I believe other scripting languages use a keyword
like 'my' or 'var' to declare the untyped variable. It occurs to me however
that Java already has a keyword that means "no type" and that is 'void'.
What about allowing the following? 'void x;' or 'void x = 5;' as a
declaration of an untyped local variable named 'x'?
I doubt many people would really have reason to use this very often, but it
seems natural to me. I'm open to suggestions (including 'my' or 'var'...
just hate to add another keyword).
Please let me know what you think about this. If we're going to do it I
I'd like to do it from the very first 1.3 release.
This SF.NET email is sponsored by: FREE SSL Guide from Thawte
are you planning your Web Server Security? Click here to get a FREE
Thawte SSL guide and find the answers to all your SSL security issues.
Beanshell-users mailing list