Thursday, April 1, 2010

Re: calling dictionary functions with call()

Am 01.04.2010 04:31, schrieb Ted:
> Hello,
> I'm hoping to call a "static" dictionary function using call(). By
> "static" I mean that the keyword 'dict' is not used in the function's
> definition. I use this nomenclature in the hopes that the effect of
> this keyword is to declare a static member function as is possible in
> java/C++/etc, ie to restrict the scope of the function name without
> requiring it to be associated with an object. However this doesn't
> seem to work. For example:
>
> " Setup:
> let testdict = { }
> funct! testdict.funct()
> echo "called"
> endfunct
>
> " Tests:
> " Following each line is an indented comment containing its
> " output in message land, ie what was echoed.
> call testdict.funct()
> " called
> echo testdict.funct
> " 667
> echo string(testdict.funct)
> " function('667')
> echo function('667')
> " E475: Invalid argument: 667
> echo function('testdict.funct')
> " testdict.funct
> call call(testdict.funct, [ ])
> " E725: Calling dict function without Dictionary: 667

If it wants a dict, then give it a dict:

call call(testdict.funct, [], {})

> " Same deal if there's an intermediate variable involved.
> let TestdictDotFunct = testdict.funct
> echo TestdictDotFunct
> " 667
> echo string(TestdictDotFunct)
> " function('667')
> call TestdictDotFunct()
> " E725: Calling dict function without Dictionary: 667

call call(TestdictDotFunct, [], {})

>> From the help topic *E725*:
> It is also possible to add a function without the "dict" attribute
> as a Funcref to a Dictionary, but the "self" variable is not
> available then.

Applies when the function is global:
func! GlobalFunc()
echo exists("self")
endfunc
let testdict.onemorefunc = function("GlobalFunc")

> So logic would seem to indicate that if "self" is not available, then
> it should be possible to call the function referenced by the Funcref
> without a Dictionary. However this doesn't seem to be the case. Am I
> missing something?

It's because the following implies the dict attribute:

let testdict = { }
funct! testdict.funct()
...

HTH (no warranty),
Andy

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

To unsubscribe, reply using "remove me" as the subject.

No comments: