getting the source code for an anonymous function

10 views
Skip to first unread message

Ted

unread,
Aug 3, 2010, 9:35:55 PM8/3/10
to vim_use
I asked [this question] a while back on stackoverflow.com: how to get
the source code for an anonymous VimL function.

The response that I got explained that doing e.g.

:function {42}

would display the source code for anonymous function #42.

This is particularly useful when debugging errors or exceptions in
anonymous functions, as the function number is the only way to
identify the function.

However neither I nor the responder seem to be able to find the
documentation for this feature. Is vim intended to work this way?
Can this command be expected to continue to function in subsequent
versions of vim?

[this question]: http://stackoverflow.com/questions/2626137

Bram Moolenaar

unread,
Aug 4, 2010, 7:20:47 AM8/4/10
to Ted, vim_use

Ted cecinemapasderange wrote:

You are not supposed to use the function number. Use the name under
which it is stored. E.g. for the example:

:let mydict = {'data': [0, 1, 2, 3]}
:function mydict.len() dict
: return len(self.data)
:endfunction
:echo mydict.len()

You can use
:function mydict.len

--
A consultant is a person who takes your money and annoys your employees while
tirelessly searching for the best way to extend the consulting contract.
(Scott Adams - The Dilbert principle)

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\ download, build and distribute -- http://www.A-A-P.org ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///

Andy Wokula

unread,
Aug 4, 2010, 9:26:40 AM8/4/10
to vim...@googlegroups.com
Am 04.08.2010 13:20, schrieb Bram Moolenaar:
> Ted cecinemapasderange wrote:
>
>> I asked [this question] a while back on stackoverflow.com: how to get
>> the source code for an anonymous VimL function.
>>
>> The response that I got explained that doing e.g.
>>
>> :function {42}
>>
>> would display the source code for anonymous function #42.
>>
>> This is particularly useful when debugging errors or exceptions in
>> anonymous functions, as the function number is the only way to
>> identify the function.
>>
>> However neither I nor the responder seem to be able to find the
>> documentation for this feature. Is vim intended to work this way?
>> Can this command be expected to continue to function in subsequent
>> versions of vim?
>>
>> [this question]: http://stackoverflow.com/questions/2626137

I'd like to say it's a cool workaround, thanks for sharing!

> You are not supposed to use the function number. Use the name under
> which it is stored. E.g. for the example:
>
> :let mydict = {'data': [0, 1, 2, 3]}
> :function mydict.len() dict
> : return len(self.data)
> :endfunction
> :echo mydict.len()
>
> You can use
> :function mydict.len

The obvious question is: If all you get from the error message is a
number, how do you know this number represents mydict.len()?

The topic has come up before:

getting more information about numbered functions in traces?
http://groups.google.com/group/vim_use/browse_thread/thread/63f4de4d7afd900/b8357da9088ad333

And there is an item in the todo list (:h todo):

It's difficult to debug numbered functions (function in a Dictionary). Print
the function name before resolving it to a number?
let d = {}
fun! d.foo()
echo "here"
endfun
call d.foo(9)

--
Andy

zheng bin

unread,
Aug 29, 2010, 11:15:20 AM8/29/10
to vim...@googlegroups.com
A

2010/8/4 Andy Wokula <anw...@yahoo.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

Reply all
Reply to author
Forward
0 new messages