On Fri, 13 Oct 2006 at 7:29pm, Martin Krischik wrote:

> Am Freitag, 13. Oktober 2006 17:22 schrieben Sie:
>
> > function mydict.len() dict
> >     let var_in_func = 2
> >     let s:script_var_in_func = 2
> >    return len(self.data)
> > endfunction
>
> This syntax is only suitable for what is called an Meta-Class or a
> Singelton-Pattern. It is not suitable for real OO programming. See:
>
>
http://en.wikibooks.org/wiki/Learning_the_vi_editor/Vim#Object_orientated_programming
>
> As you can see the creation of a true class is a lot more complex and - more
> importantly - difficult to parse for ctags - at least when more then one
> class is defined inside a script file. Mind you Ada suffers a similar problem
> as Ada too allows for more then one class to be defined inside on package.
>
> Martin

An alternative approach to what was suggested (assembling an instance),
is to use it like a template or prototype-based language. I have been
using this approach to create a factories like this:

let plug#factory = {...}
function! plug#factory.getX()
function! plug#factory.doY()

function! plug#factory.new()
  let newobj = deepcopy(self)
  unlockvar! newobj
  return newobj
endfunction

lockvar! plug#factory

The usage is as follows, which is very close to OO programing. Of
course, you can't do inheritance, but you can solve some problems by
delegation. Polymorphism and inheritance can also be achieved by
doing some manual assembly new(), but this will be an unnecessary
overhead for most problems that we will try to solve in Vim.

let obj = g:plug#factory.new()
call obj.doY()

The factory object is locked once all the dictionary functions are
defined. This avoids accidentally modifying the factory instance
instead of the newly created ones. The newly created object will have
all the members of the original including any fields (non-Funcref
keys) and member functions (numbered or Funcref keys).

-- 
Thanks,
Hari

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

Reply via email to