Scoping in nested functions: How should it work?

4 views
Skip to first unread message

Ian Tegebo

unread,
Jan 8, 2008, 4:00:22 PM1/8/08
to Vim Developers
I'm exploring functional programming with vimL and immediately ran into
seemingly counter-intuitive behavior:

------------------------------------
function! Outer()
let outer_var = 0
function! Inner()
let outer_var += 1
endfunction
call Inner()
echo outer_var
endfunction

call Outer()
-------------------------------------

The result, when I run this from 7.1.156, is that 'Inner' reports
'outer_var' as
unknown. I was expecting '0' but would eventually like to have my 'Inner'
use 'outer_var' as state via dictionaries or some other mechanism. It's
disturbing that 'outer_var' is not available to 'Inner'.

Am I missing something? Is this the intended behavior? If so, where else
can I read more about scoping? I've been reading usr_41.txt and eval.txt.

--
Ian Tegebo

Tony Mechelynck

unread,
Jan 8, 2008, 4:55:36 PM1/8/08
to vim...@googlegroups.com, Vim Developers

Inside a function, a variable name without a scope is the same as l: -- i.e.,
local to the _current_ function. Now there is no outer_var defined _inside_
the Inner() function, so it properly gives an error.

Variables local to Outer() are not accessinle to Inner(). From both of them
you can access most variables except function arguments and function-local
variables.

See ":help internal-variables" (and what it resends to) for details.


Best regards,
Tony.
--
If you cannot convince them, confuse them.
-- Harry S Truman

ap

unread,
Jan 8, 2008, 5:53:13 PM1/8/08
to vim_dev


On Jan 8, 10:00 pm, "Ian Tegebo" <ian.teg...@gmail.com> wrote:
> I'm exploring functional programming with vimL and immediately ran into
> seemingly counter-intuitive behavior:
>
> ------------------------------------
> function! Outer()
> let outer_var = 0
> function! Inner()
> let outer_var += 1
> endfunction
> call Inner()
> echo outer_var
> endfunction
>
> call Outer()
> -------------------------------------
>
> The result, when I run this from 7.1.156, is that 'Inner' reports
> 'outer_var' as
> unknown. I was expecting '0' but would eventually like to have my 'Inner'
> use 'outer_var' as state via dictionaries or some other mechanism. It's
> disturbing that 'outer_var' is not available to 'Inner'.
>
> Am I missing something?
>
There are no closures in what you call vimL. Your code is equivalent
to
this :

------------------------------------
function! Outer()
let outer_var = 0
call Inner()
echo outer_var
endfunction

function! Inner()
let outer_var += 1
endfunction

call Outer()
-------------------------------------

As Tony pointed out, you have 2 different local variables.

-ap

ap

unread,
Jan 8, 2008, 6:00:06 PM1/8/08
to vim_dev


On Jan 8, 10:00 pm, "Ian Tegebo" <ian.teg...@gmail.com> wrote:
> I'm exploring functional programming with vimL ...

Btw where did you get the idea vimscript is a functional language ?

-ap

Ben Schmidt

unread,
Jan 8, 2008, 6:42:09 PM1/8/08
to vim...@googlegroups.com
> Your code is equivalent
> to this :
>
> ------------------------------------
> function! Outer()
> let outer_var = 0
> call Inner()
> echo outer_var
> endfunction
>
> function! Inner()
> let outer_var += 1
> endfunction
>
> call Outer()
> -------------------------------------

Except that the Inner function is defined before calling Outer, not during the
call of Outer.

It might help to realise that in Vim, 'function' is a command that defines a
function in global scope (or script scope sometimes). You can run this command
whenver you like to define a function, and it always does the same thing. It is
NOT a program syntactical construct that defines a function at the point where it
is placed in the code.

Ben.

Send instant messages to your online friends http://au.messenger.yahoo.com

Ian Tegebo

unread,
Jan 8, 2008, 7:40:41 PM1/8/08
to vim...@googlegroups.com
Apologies for not being more clear. I meant to say:

"I'm exploring a functional programming style within vimL..."

Also, what do people call the language vim uses for extensions, e.g.
vimL(anguage), vim-script?

--
Ian Tegebo

Ben Schmidt

unread,
Jan 8, 2008, 7:45:06 PM1/8/08
to vim...@googlegroups.com

Usually vimscript.

Reply all
Reply to author
Forward
0 new messages