Sunday, May 30, 2021

Re: Vim9: how to invoke callback with additional arguments?

> I am trying to understand how to invoke a callback in Vim9 script.
> I have started from this example (from the excellent lacygoill's wiki):
>
> vim9script
>
> def Callback(_j: job, _e: number)
> echom 'callback'
> enddef
>
> def Func()
> job_start(['/bin/bash', '-c', ':'], {exit_cb: Callback})
> enddef
>
> Func()
>
> That's fine, but now suppose that I want to pass additional arguments to
> Callback(). What is Vim9's equivalent of the following?
>
> fun Callback(value, _j, _e)
> echom 'callback with value: ' .. a:value
> endf
>
> fun Func()
> call job_start(['/bin/bash', '-c', ':'],
> \ {'exit_cb': function('Callback', [42])})
> endf
>
> call Func()

I don't think there is a difference, you can still use function() to
create a partial.

Is there a "Vim9 way" that would be better? You could use a lambda:

call job_start(['/bin/bash', '-c', ':'],
\ {'exit_cb': (j, e) => Callback(42, j, e)})

It looks a bit nicer. I haven't tried it, you may need to add types.

--
The Law of VIM:
For each member b of the possible behaviour space B of program P, there exists
a finite time t before which at least one user u in the total user space U of
program P will request b becomes a member of the allowed behaviour space B'
(B' <= B).
In other words: Sooner or later everyone wants everything as an option.
-- Vince Negri

/// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\
/// \\\
\\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/202105302018.14UKI2951871595%40masaka.moolenaar.net.

No comments: