2

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?

1 Answer 1

3

I don't think there is such an example. The help for function() is the same as the help for three errors: E700, E922 and E923. None of these refer to a situation like the one the text seems to describe:

  • E700: Unknown function

    :call function('six',[1],[1])
    E700: Unknown function: six
    
  • E922: expected a dict

    :call function('sin',[1],[1])
    E922: expected a dict
    
  • E923: Second argument of function() must be a list or a dict

    :call function('sin',1)
    E923: Second argument of function() must be a list or a dict
    

Since no other errors are listed for function(), I think the help is probably describing something like C's undefined behaviour rather than an explicit error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.