vim9script : how to port mechanism of 'functions'dictionary ~ classes

15 views
Skip to first unread message

N i c o l a s I I T h e B l o o d y

unread,
Dec 10, 2022, 4:37:53 AM12/10/22
to vim_dev
Hello Bram,

Hope your health is back to its best.

Question: Until the classes are implemented in vim9script, how to replace the mechanism of 'functions'dictionary ' historically used in legacy vimscript in vim9script?

Here is an example of a state machine used in tagbar https://github.com/preservim/tagbar/blob/master/autoload/tagbar/state.vim

Thanks
Nicholas

N i c o l a s I I T h e B l o o d y

unread,
Dec 10, 2022, 4:43:34 AM12/10/22
to vim_dev
Sorry I missed this in the vim9 help :)

*vim9-no-dict-function*
Later classes will be added, which replaces the "dict function" mechanism.
For now you will need to pass the dictionary explicitly: >
    def DictFunc(self: dict<any>, arg: string)
       echo self[arg]
    enddef
    var ad = {item: 'value', func: DictFunc}
    ad.func(ad, 'item')

You can call a legacy dict function though: >
    func Legacy() dict
      echo self.value
    endfunc
    def CallLegacy()
      var d = {func: Legacy, value: 'text'}
      d.func()
    enddef

Reply all
Reply to author
Forward
0 new messages