From :h 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)
The examples are incomplete, so I tried this:
let dict = { 'name': 'trees' }
fu! dict.Func(n)
echo a:n.' '.self.name
endfu
let FuncWithArg = function(dict.Func, [3])
call FuncWithArg()
It works as expected and displays 3 trees
.
Then, I tried this:
let Broken = function(dict.Func, [3], dict)
call Broken()
It works too, and displays 3 trees
. But if this example is really equivalent to the one given in the help, it shouldn't work, since the help says that the {dict} argument is not allowed
, when {name}
is a partial.
I think the reason why it works, while it shouldn't, is because in my example, dict.Func
isn't a partial.
So, I tried to rewrite these 2 examples, so that dict.Func
is a partial:
fu! MyFunc(i,j) dict
echo (a:i + a:j).' '. self.name
endfu
let dict = { 'name': 'trees', 'Func': function('MyFunc', [1]) }
let FuncWithArg = function(dict.Func, [2])
call FuncWithArg()
let Broken = function(dict.Func, [2], dict)
call Broken()
Both commands, call FuncWithArg()
and call Broken()
, work and display 3 trees
.
But again, the 2nd one shouldn't work according to the {dict} argument is not allowed
. So, I thought that maybe what the help meant was not:
let Broken = function(dict.Func, [arg], dict)
… but:
let Broken = function(dict.Func, [arg], other_dict)
So, I tried to find another example where dict
is replaced with other_dict
:
fu! MyFunc(i,j) dict
echo (a:i + a:j).' '. self.name
endfu
let dict = { 'name': 'trees', 'Func': function('MyFunc', [1]) }
let other_dict = { 'name': 'mountains', 'Func': function('MyFunc', [1]) }
let FuncWithArg = function(dict.Func, [2])
call FuncWithArg()
let Broken = function(dict.Func, [2], other_dict)
call Broken()
Both commands, call FuncWithArg()
and call Broken()
, work and display 3 trees
and 3 mountains
respectively.
I haven't been able to come up with an example where call Broken()
would raise an error. Can someone provide one?