November 2018
Beginner
330 pages
7h 21m
English
While Vim doesn't explicitly contain classes, dictionaries support having methods on them, supporting the object-oriented programming paradigm. There are two ways to define a method on a dictionary.
Given an existing dictionary, animal_names:
let animal_names = { \ 'cat': 'Miss Cattington', \ 'dog': 'Mr Dogson', \ 'parrot': 'Polly' \ }
You could do the following to add a method to it:
function animal_names.GetGreeting(animal) return self[a:animal] . ' says hello'endfunction
You can now execute the function:
:echo animal_names.GetGreeting('cat')Miss Cattington says hello
You can use self (just like in Python!) to refer to dictionary keys.
In the previous example, GetGreeting becomes a callable dictionary key. Effectively, ...
Read now
Unlock full access