Hi everyone! I have some questions about argument types of self-
defined functions, please help me:

>From my understanding, Vim "understands" any type you passed to a self-
defined function and makes no pre-assumption about it, but now I have
doubt about this.

Here is the pseudocode of functions I wrote in my script, I hope they
can be used to output objects of any type/sturcture to a file:

"""""""""""""""""""""""""""""""""""""""""""""""""
" name:  name of an object, could be of any type
" value: its value
" level: its logic level

function! s:OutputObject(name, value, level)

  let outLines= [] "lines to be output to a file

  let pfx= repeat("\t", a:level) "line prefix

  if type(a:value) == Number or Float or String
    "add {pfx + name + ':' + value} to outLines[]
  elseif type(a:value) == Funcref
    "add {pfx + name + '->' + value} to outLines[]
  elseif type(a:value) == List
    "add {prefix + name + '[]'} to outLines[]
    "then recursively calls itself for each item
    "with one more level and append the returned
    "list to outLines[]
  elseif type(a:value) == Dictionary
    "add: prefix . 'name' .'{}' to outLines[]
    "then recursively calls itself for each item
    "with one more level and append the returned
    "list to outLines[]
  endif

  return outLines

endfunction
"""""""""""""""""""""""""""""""""""""""""""""""""

And the caller function would be like this:

"""""""""""""""""""""""""""""""""""""""""""""""""
function! s:StoreProgramStatus()

  let lines= []

  call extend(lines,
  \ s:OutputObject('obj1', obj1, 0))

  call extend(lines,
  \ s:OutputObject('obj2', obj2, 0))

  call extend(lines,
  \ s:OutputObject('obj3', obj3, 0))

  call writefile(lines, myfile)

endfunction
"""""""""""""""""""""""""""""""""""""""""""""""""

Generally, the first function accepts an object 'value', check its
type, and take action accordingly.

I hope to use these functions to store the status of a running script,
I have many complex data structures in my script: Lists, Dicts, Lists
of Dicts, Dicts of Lists of Dicts, etc, etc ... and with their
couterparts s:LoadProgramStatus() & s:InputObject(), the status can be
restored next time I run the same script.

The output of these functions looks almost ok, but occasionally (4 out
of hundreds of thousands of lines) Vim complains about the wrong
augument type (error E706). For example, these are pieces of the
output lines generated by the function, I left them unmodified:

s:mainWin{}
        \src\main.cc{}
                location[]
                crtIdx[]
                        [0] :   -1
                        [1] :   -1
                lastJump[]
                refName[]
                crtView[]               !!!
                tagIdx[]
s:history[]
        [0]{}
                depth :   0
                lastTgt :   -1
                out :   -1              !!!
                source :   \src\main.cc
                previous :   0
                in :   6469
                next :   6469           !!!

The lines marked with '!!!' are wrong: in my script, "crtView" is a
Number, but Vim takes it for a List; and "out" and "next" are Lists,
but was taken for Numbers.

I noticed that the wrong line would just take the status of the
previous one, so I guess that Vim would actually make pre-assumptions
about the argument type for different call of the same function. If
you calls a function with a certain argument type, next time Vim would
expect the same type.

Here's my questions:
Can you confirm my guess? Am I wrong at all trying to write a function
that takes argument of different types?
If that kind of functions actually can be written in Vim scirpt, then
how to make the above functions right?
Besides, Do you know any convenient way of storing running status of a
script -like browsing history in a browsing environment- so it can be
restored in the next run?

Please give your advise, thanks !


--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply via email to