2016-05-18 12:06 GMT+03:00 Andrey Gavrikov <[email protected]>:
> To actually do this one needs to use `get(dict, 'func')`
>
> Does not this imply that 'dict' is available at the point when 'func' is
> about to be invoked?
>
> if I was to extend my original example and instead of calling call
> obj2.func2() directly we had something like this:
>
> " obj2.func2 will be called when channel is closed
> call job_start("ping -c 3 google.com", {"close_cb": obj2.func2})
>
> here is a full version
>
> function! Callback_Test()
> let obj1 = {"test": 'test1', "other_test": "other_test"}
> function obj1.func1() " next line will use value from wrong scope
> echomsg "inside obj1.func1: test=".self.test " next line will
> result in: 'Key not present in Dictionary: other_test'
> echomsg "inside obj1.func1: other_test=".self.other_test
> endfunction
>
> let obj2 = {"test": 'test2', 'callback': obj1.func1}
> function obj2.func2(...)
> echomsg "inside obj2.func2: ".self.test
> call self.callback()
> endfunction
> "call obj2.func2()
> call job_start("ping -c 3 google.com", {"close_cb":
> obj2.func2})endfunction
>
> In this instance - is there a way to ensure that when obj1.func1 is
> finally called we have access to its obj1 'dict' in order to be able to do
> get(obj1,
> 'func1')
>
Since you store `obj1.func1` in obj2 you need to do `get(obj2,
'callback')` to get partial that is attached to `obj1` because indexing
`obj2` (i.e. `obj2.callback` or `self.callback` in your code) creates new
partial with `obj2` attached in place of `obj1`. I do not see why you think
you need access to `obj1`, expression `obj1.func1` creates a partial which
references this dictionary and you store this partial in `obj2` (and you
can use `pyeval('vim.bindeval("obj2")["callback"].self')` to get `obj1`
back if needed).
But if you do `get(obj1, 'func1')` you will *not* get a partial, you will
get a funcref without `self` dictionaries attached: when defining a
function no partial is created. Calling such funcref needs explicit `self`
(i.e. `call(get(obj1, 'func1'), [], obj1)`, not `get(obj1, 'func1')()`).
> —
> You are receiving this because you commented.
> Reply to this email directly or view it on GitHub
> <https://github.com/vim/vim/issues/812#issuecomment-219968340>
>
> --
> --
> 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.
>
--
--
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.