ih4cku / blog

deprecated, Git issues are great for writing blogs :)
2 stars 0 forks source link

vim dictionary function #72

Open ih4cku opened 8 years ago

ih4cku commented 8 years ago

Dictionary function

When a function is defined with the dict attribute it can be used like a member function in object oriented programming.

function MyLen() dict
  return len(self.data)
endfunction
let mydict = {'data': [0, 1, 2, 3], 'len': function("MyLen")}
echo mydict.len()

In the above codes, function("MyLen") is used as a Funcref.

Also you can directly assign the function to a dictionary.

let mydict = {'data': [0, 1, 2, 3]}
function mydict.len() dict
  return len(self.data)
endfunction
echo mydict.len()