tags:

views:

51

answers:

1

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 put the function name in the class namespace but allow it to be called without referencing 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

" 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

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.

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?

Vim version info:

$ aptitude show vim-gnome
Package: vim-gnome
State: installed
Automatically installed: no
Version: 2:7.2.245-2ubuntu2

Edit:

What I really want to do is to allow dictionary functions to make calls to either other dictionary functions or to non-dictionary functions transparently.

This is possible by passing self as the third parameter in the call() invocation (thanks @ZyX); this parameter is just ignored if a non-dictionary function is being called. I found this kind of surprising because it's very different from how object-oriented constructs in other languages work. But I guess vimscript's OOP stuff is more like C than C++ in a lot of ways.

EG

let dict = { }
funct! dict.func(arg)
  echo 'dict.func called.'
  echo 'argument: '.a:arg
endfunct
funct! dict.callfunc_passself(arg)
  call call(self.func, [a:arg], self)
endfunct
funct! dict.callfunc_nopassself(arg)
  call call(self.func, [a:arg])
endfunct

funct! Func(arg)
  echo "Func called."
  echo 'argument: '.a:arg
endfunct


call dict.callfunc_passself('argument supplied to dict.callfunc')
  " dict.func called.
  " argument: argument supplied to dict.callfunc
call dict.callfunc_nopassself('argument supplied to dict.callfunc')
  " E725: Calling dict function without Dictionary: 37

let dict.func = function('Func')
call dict.callfunc_passself('argument supplied to dict.callfunc')
  " Func called.
  " argument: argument supplied to dict.callfunc
call dict.callfunc_nopassself('argument supplied to dict.callfunc')
  " Func called.
  " argument: argument supplied to dict.callfunc

So as long as the call to the [non-]dictionary function from within the dictionary function passes self as the third parameter to call, then this will work quite well.

This means that in addition to being able to build inheritance structures using cloned-and-modified dictionaries (ie copy prototyping) it's possible to have some of the methods be references to non-dictionary functions.

+1  A: 

All dictionary functions require some dictionary provided as the third argument to call(). All anonymous functions are dictionary functions. All functions that are declared using function dict.func() are anonymous. Just supply {} as the third argument to all call() calls and forget about this.

And reread the documentation. Self is available in your function. Documentation says that self won't become available if you do this: let dict.func=function("Foo").

ZyX
I missed that third parameter to `call()`; thanks for that. I guess that could be a workaround. To clarify: I don't want `self` to be available in the function. I just want to key a hash entry to an anonymous function. Mostly I don't want to have to clutter the namespace with a function name that should just be a hash entry.
intuited
Documentation (`:help E124`) says `When the [dict] argument is added, the function must be invoked through an entry in a |Dictionary|. The local variable "self" will then be set to the dictionary.` However it seems like that argument doesn't actually do anything, since it's not possible to call such a function without passing a dictionary.
intuited
Oh okay, I see now. `dict` is added to functions that doesn't use the form `dict.func`. However I guess it's not possible to declare a function using the `dict.func` form, aka an "anonymous function", that can be called outside of the context of a dictionary.
intuited