import { fn } from "https://deno.land/x/ddc_vim@v4.0.2/deps.ts";
const { function_ } = fn;
Return a Funcref
variable that refers to function {name}.
{name} can be the name of a user defined function or an
internal function.
{name} can also be a Funcref or a partial. When it is a partial the dict stored in it will be used and the {dict} argument is not allowed. E.g.:
let FuncWithArg = function(dict.Func, [arg])
let Broken = function(dict.Func, [arg], dict)
When using the Funcref the function will be found by {name},
also when it was redefined later. Use funcref()
to keep the
same function.
When {arglist} or {dict} is present this creates a partial. That means the argument list and/or the dictionary is stored in the Funcref and will be used when the Funcref is called.
The arguments are passed to the function in front of other
arguments, but after any argument from method
. Example:
func Callback(arg1, arg2, name)
...
let Partial = function('Callback', ['one', 'two'])
...
call Partial('name')
Invokes the function as with:
call Callback('one', 'two', 'name')
With a method
:
func Callback(one, two, three)
...
let Partial = function('Callback', ['two'])
...
eval 'one'->Partial('three')
Invokes the function as with:
call Callback('one', 'two', 'three')
The function() call can be nested to add more arguments to the Funcref. The extra arguments are appended to the list of arguments. Example:
func Callback(arg1, arg2, name)
"...
let Func = function('Callback', ['one'])
let Func2 = function(Func, ['two'])
"...
call Func2('name')
Invokes the function as with:
call Callback('one', 'two', 'name')
The Dictionary is only useful when calling a "dict" function. In that case the {dict} is passed in as "self". Example:
function Callback() dict
echo "called for " .. self.name
endfunction
"...
let context = {"name": "example"}
let Func = function('Callback', context)
"...
call Func() " will echo: called for example
The use of function() is not needed when there are no extra arguments, these two are equivalent, if Callback() is defined as context.Callback():
let Func = function('Callback', context)
let Func = context.Callback
The argument list and the Dictionary can be combined:
function Callback(arg1, count) dict
"...
let context = {"name": "example"}
let Func = function('Callback', ['one'], context)
"...
call Func(500)
Invokes the function as with:
call context.Callback('one', 500)
Returns 0 on error.
Can also be used as a method
:
GetFuncname()->function([arg])