If I source:
let Mydict = { 'data': [0, 1, 2, 3] }
fu! Mydict.mylen()
return len(self.data)
endfu
Then, I can execute echo Mydict.mylen()
which returns 4
because it's the length of the list [0,1,2,3]
. I didn't have to give the dict
attribute to the function Mydict.mylen()
.
If I source:
fu! MyFunc() dict
return len(self.data)
endfu
let mydict = { 'data': [0, 1, 2, 3], 'mylen': function('MyFunc') }
Then, I can execute echo mydict.mylen()
which, again, returns 4
, for the same reason as before. However, this time, I had to give the dict
attribute to the function MyFunc()
. Without dict
, it would have raised the errors:
E121: Undefined variable: self
E116: Invalid arguments for function len(self.data)
E15: Invalid expression: len(self.data)
If I source:
fu! Order() dict
echo 'eat your ' . self.name
endfu
let food = { 'name': 'vegetables' }
let BuildOrder = function('Order', food)
Then, I can execute call BuildOrder()
, which displays eat your vegetables
. I had to give the dict
attribute to the function Order()
, for the same reason as before.
If I source:
fu! Describe(count, adj) dict
echo a:count.' '.a:adj.' '. self.name
endfu
let animal = { 'name': 'piggies' }
let Description = function('Describe', [ 3 ], animal)
Then, I can execute call Description('little')
, which displays 3 little piggies
. And again, I had to give the dict
attribute to the Describe()
function.
dict
is only needed in the last 3 of the 4 previous snippets. What does Vim do internally, which explains that dict
is not needed in the first one, but is in all the others?
And whatever it does, why doesn't it do it in the last 3 snippets? What would happen if it did it all the time, or never?