On Wed, Oct 31, 2012 at 8:33 PM, Tim Chase <vim@tim.thechases.com> wrote:
> On 10/31/12 06:05, Dotan Cohen wrote:
>> On Wed, Oct 31, 2012 at 12:55 PM, Tony Mechelynck wrote:
>>> Yes, and in addition, if you don't use a scope prefix Vim
>>> implies l: if you're inside a function and g: otherwise.
>>>
>>> See :help internal-variables
>>
>> Thank you, that is not consistent with other programming
>> environments that I am familiar with. Very good to know!
>
> Python happens to work this way:
>
> x = 42
> def foo():
> x = 32
> return x
> print foo()
> print x
>
> prints 32 followed by 42 (the x remains local to the function).
Right, this is how most C-based languages work. I don't know if Python
is C-based (I think so), but this is what I expect. What I don't
expect is that x would be recognized inside the foo() declaration if
it was only defined outside, as Tony mentions is the case with VIM.
Let's try:
>>> x = 42
>>> print x
42
>>> def foo():
... print x
...
>>> print x
42
>>> foo()
42
Wow! This would be at minimum a compiler warning at at worse a huge
security flaw in other languages. I happen to have PHP handy:
php > $x = 42;
php > echo $x . PHP_EOL;
42
php > function foo() {
php { echo $x . PHP_EOL;
php { return true;
php { }
php > echo $x . PHP_EOL;
42
php > foo();
PHP Notice: Undefined variable: x in php shell code on line 2
The variable $x was not declared in foo(), and is not valid there.
Now, to check my suspicions:
- neptune:c$ cat scopeTest.c
#include <stdio.h>
int foo();
int main() {
int x = 42;
printf("%d", x);
foo();
return 0;
}
int foo() {
printf("%d", x);
}
- neptune:c$ gcc scopeTest.c
scopeTest.c: In function 'foo':
scopeTest.c:15:15: error: 'x' undeclared (first use in this function)
scopeTest.c:15:15: note: each undeclared identifier is reported only
once for each function it appears in
- neptune:c$
Nope, C won't allow it either.
In both PHP and C there are ways to explicitly make x global, but it
is not implicit.
> I've noticed little bits of Python show up in VimScript--such as
> array slicing, negative-indexing, and first-class(ish) functions.
> Not that I mind, as Python is my preferred language. :-)
>
> There may be others where this scope is the same (pascal comes to mind)
>
I've actually never touched Pascal, nor Cobol or Fortran for that matter.
Thank you for the information regarding Python. I do dabble in Python
occasionally, and things like this I should know. I love Python, but
VIM is ill-equipped to handle bracketless languages out of the box!
--
Dotan Cohen
http://gibberish.co.il
http://what-is-what.com
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
No comments:
Post a Comment