Nikolay Pavlov wrote:
> >> 2016-03-13 18:06 GMT+03:00 Bram Moolenaar <[email protected]>:
> >> >
> >> > I have a note about adding a way that a callback can be invoked at a
> >> > certain time. I would like to know how this is going to be used, what
> >> > the requirements for this feature are.
> >> >
> >> > I imagine there are two kinds of timers:
> >> >
> >> > 1. One-time: After N msec the callback is invoked.
> >> > 2. Repating: invoke the callback every N msec
> >> >
> >> > For both this will happen while waiting for a character, thus the actual
> >> > wait time will be longer if the user is typing.
> >> >
> >> > Example:
> >> >
> >> > let timer = timer_add({time}, {callback} [, {repeats} [,
> >> > {cookie}]])
> >> > call timer_stop(timer)
> >> >
> >> > func MyTimerCallback(timer, cookie)
> >> >
> >> > Would that satisfy the need?
> >> >
> >> > I think it would be good that if the callback causes an error the timer
> >> > is stopped.
> >>
> >> I would say that dictionary is better then four arguments:
> >>
> >> 1. It enables feature used by Neovim for callbacks: dictionary that
> >> defined timer is passed as a `self` dictionary to a dictionary
> >> function, while in your variant dictionary functions cannot be used at
> >> all.
> >> 2. This allows adding more options in the future. E.g. alter error
> >> handling, silence function or add a timeout after which callback is
> >> interrupted.
> >> 3. Dictionary may serve both as `timer` and as a `cookie`.
> >>
> >> E.g.
> >>
> >> let def = {
> >> \ 'interval': Number|Float (seconds),
> >> \ 'callback': String|Funcref,
> >> \ 'repeats': Number (optional),
> >> \ 'Cookie': … (keys matching `^[a-z]\+$` are reserved for Vim)}
> >> call timer_add(def)
> >> call timer_stop(def)
> >>
> >> function Callback() dict
> >> endfunction
> >
> > That's very different from how existing functions work.
> >
> > The "cookie" can be a dictionary, thus one can use that. Passing the
> > function argument back in to the callback looks weird. What we would
> > actually want is a closure. Then the cookie is not needed.
>
> Argument being passed is a dictionary containing function, not the
> function. And this does not look weird at all, every OOP library
> written in VimL I saw does the same thing.
What is weird is that a function is needed and you pass a dictionary
containing a function. The only reason Vim users have been doing this
is that there was no other way. It's not that this is a good way to do
it.
In this case the "callback" item of the dictionary defines the function.
Why not another? What if there are there several functions? Anyway,
I think it's weird.
Another objection to using the dictionary both for options and for
passing to the callback is that it's not clear what happens if some of
the option values is changed. Does that mean the changed value will be
used?
I think it's better to separate options from any dictionary related to
the callback. And there are many other places callbacks are used where
binding it with arguments or a dictionary is useful.
> > We could make this generic:
> > let closure = function('Callback', arg1, arg2)
> > Would invoke:
> > func Callback(arg1, arg2, other-args)
>
> This is currying (partial application), not closure. Closure is
>
> function Outer()
> let l:test = 1
> let l:d = {}
> function d.inner()
> return l:test
> endfunction
> return d.inner
> endfunction
>
> let Closure = Outer()
> echo Closure()
Yeah, I know it's not a real closure. But it's the best term that I
though of. Never heard of currying. Looking it up finds something
else... Perhaps "partial" is a better fit. Not sure how many people
know what that means.
> > We might need more options later, that's true. But the time and
> > callback are always needed. Thus I would prefer:
> >
> > let timer = timer_start({time}, {callback}, {options})
>
> Do you mean that `options` here will be `self`? It is fine as long as
> dictionary functions can be used.
No, they are options. If a dictionary function is to be used we need a
way that works for any callback. Perhaps:
let callback = function('Callback', dict, arg1, arg2)
Nicer would be if we can do:
let callback = function(dict.Callback, arg1, arg2)
--
Micro$oft: where do you want to go today?
Linux: where do you want to go tomorrow?
FreeBSD: are you guys coming, or what?
/// Bram Moolenaar -- [email protected] -- http://www.Moolenaar.net \\\
/// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\ an exciting new programming language -- http://www.Zimbu.org ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///
--
--
You received this message from the "vim_dev" 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_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.