Sunday, December 15, 2013

Re: retrieve variable name


On Dec 16, 2013 12:01 AM, "Alexandre Hoïde" <alexandre.hoide@gmail.com> wrote:
>
>   Hi and thanks to all of you for your help.
>
> On Sun, Dec 15, 2013 at 09:12:21PM +0400, Nikolay Pavlov wrote:
> > Not possible.
> >
> > This looks like XY problem. What for do you need such thing?
>
>   I probably don't. I am not a dev and a noob at Vim scripting, so I try
> things.
>
>   Here, I was trying to do something like this :
>
> define some data -->
> let s:data_1 = {
>       \  'a_key': 'some_data'
>       \, 'another_key': 'data...'
>       ...
>       \}
>
> let s:data_2 = {
>       \  'a_key': 'some_other_data'
>       "similar (but not necessarily strictly identical) structure as data_1...
>       \}
> .
> .
>
> let s:data_n...
> <--
>
>   And later, put those sharing common treatments in lists. The following
> example is why I was trying to retrieve the variable ?name? :
>
> -->
> let s:candidates_for_anonymous_function_a = [ s:data_x, s:data_y, ... ]
>
> for candidate in s:candidates_for_anonymous_function_a
>   function {?name?(candidate)}.function_a()
>     return some_fancy_function(self.a_key)
>   endfunction
> endfor
> <--
>
>   Which would allow me to later use 's:data_n.function_a()'.

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.

There is no need in generating multiple functions with exactly the same body though: function is not bound to the dictionary thus you can simply generate it once and assign obtained funcref.

>
>   I thought that would be fun as a composing tool, rather flexible and,
> more importantly, complying with my way of thinking.
>
>   I could still do it if I store my variables in lists as strings, and
> later use 'eval()' on them, as suggested by Tony.
>
>   But perhaps this is just my inexperience which leads me to weird
> constructions. ^^
>
>   Thanks again 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: