Monday, December 16, 2013

Re: retrieve variable name

On Mon, Dec 16, 2013 at 07:58:40PM +0400, Nikolay Pavlov wrote:
> On Dec 16, 2013 7:14 PM, "Alexandre Hoïde" <alexandre.hoide@gmail.com>
> > 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.

Ah haaa, that's the part I had trouble understanding yesterday. So,
just to be 100% sure, like the following ?

<--
let fun_dict = {}

function! fun_dict.mfun() dict
echo self.a_key
endfunction

let mdict = { 'a_key': "Hello", 'fun': fun_dict.mfun }

" And then :
call mdict.fun()
-->

Right ?

> 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.

Ok, thanks again for those detailed explanations. I realy appreciate.

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.

No comments:

Post a Comment