Monday, December 16, 2013

Re: retrieve variable name


On Dec 16, 2013 7:14 PM, "Alexandre Hoïde" <alexandre.hoide@gmail.com> wrote:
>
> On Mon, Dec 16, 2013 at 12:38:34AM +0400, Nikolay Pavlov wrote:
> > Dictionaries are passed by reference. So dictionaries in s:data_n and
> > s:candidates... are exactly the same dictionaries and you can just use
> >
> >     function candidate.function_a()
> >
> > and it will be accesible as both s:candidates[n].function_a and
> > s:data_n.function_a.
>
>   Ah, he he… great ! Embarrassing for me but great ! :)
>
> > There is no need in generating multiple functions with exactly the same
> > body though:
>
>   That is very true. But ->
>
> > function is not bound to the dictionary thus you can simply
> > generate it once and assign obtained funcref.
>
>   No sure what (or how) you mean here. You mean something like the
> following ?
>
> <--
> function! Mfun() dict
>   echo self.a_key
> endfunction
>
> let Mref = function('Mfun')
>
> let mdict = { 'a_key': "Hello", 'fun': Mref }
> -->

Something like this. Though I tend to use anonymous functions (and only them as far as I can), thus would use temporary dict (+1 line for dict definition, -1 line for obtaining funcref: 0 total difference in lines with your code), but this is not necessary: your code also should work fine.

Note: it is generally not safe using funcrefs not inside dicts or lists: try

    fu Mref()
    endfu
    let Mref=function('abc')
. Unlike plugin functions such funcrefs tend to have more generic names (plugin functions normally look like PluginNameDoSomething or s: which is less likely to produce conflict), so if you happen to choose name F for your funcref in a plugin it is entirely possible it will be occupied by a temporary function named F user of your plugin happened to use for testing something.

Using d.Mref is always fine though.

Note 2: it is entirely possible to generate a name conflict with function s:Smth without hacking your plugin's or vim source code (but this is not possible for anonymous functions though). Nobody bothers though (not even me: I use anonymous functions for different reasons): if someone wants trouble he shall get it.

>
>   Thanks again for your help and best regards,
>
> --
>  ___________________
> | $ post_tenebras ↲ |       waouh !
> | GNU        \ /    |      /
> |          -- * --  |     o
> | $ who ↲    / \    |_-- ~_|
> | Alexandre Hoïde   |  _/| |
>  -------------------
>
> --
> --
> 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.
> For more options, visit https://groups.google.com/groups/opt_out.

--
--
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.
For more options, visit https://groups.google.com/groups/opt_out.

No comments: