Wednesday, July 6, 2011

Re: Aw: Re: vim function not first-class variable? how to create an alias for vim function ?

Reply to message «Aw: Re: vim function not first-class variable? how to create
an alias for vim function ?»,
sent 21:09:41 06 July 2011, Wednesday
by lith:

> One caveat though, IIRC if F refers to a script local function, it cannot
> be called from outside of the script context.

There are no real script-local functions:

execute frawor#Setup('0.0', {})
function s:F()
echo "Here"
endfunction
let g:F=function('<SNR>'.s:_sid.'_F')

If you know your script id you can create a reference to script-local function
that can be called outside of script context. In this case it is put by
frawor#Setup into s:_sid variable, run `:h <SID> | /s:SID' for information about
how to get script id without using frawor.

> This makes me wonder if there is a usecase, where the first argument to
> call() has to be a Funcref and cannot be a string (the name of a function).
There are anonymous functions (:h numbered-function):
let s:F={}
function s:F.func()
echo "Here"
endfunction
. It is not possible to call anonymous function without a reference to it and
you can't create the reference using function() call, so the above code
effectively restricts write/execute access to the script (unless you pass
s:F.func, s:F or s: somewhere).

Such functions are still global though.

Original message:
> Am Dienstag, 5. Juli 2011 10:41:23 UTC+2 schrieb MarcWeber:
> > Excerpts from pansz's message of Tue Jul 05 10:24:05 +0200 2011:
> > > I want to do some coding like this:
> > let F = function('Foobar')
> >
> > call call(F, [arg1, arg2], { optional self dict })
>
> One caveat though, IIRC if F refers to a script local function, it cannot
> be called from outside of the script context.
>
> This makes me wonder if there is a usecase, where the first argument to
> call() has to be a Funcref and cannot be a string (the name of a function).
>
> Regards,
> Tom

No comments:

Post a Comment