On Sun, Jan 26, 2020 at 6:57 PM Bram Moolenaar <
Br...@moolenaar.net> wrote:
>
>
> Tony wrote:
>
> > IIUC, the following is now a valid construct in newly-written or
> > updated non-"vim9script" script? (Let's say, a .vimrc which must also
> > support an older Vim version from my Linux distro)
> >
> > if exists('vim9script') == 2
>
> That should be:
> if exists(':vim9script') == 2
yes, I sent an erratum
>
> > import MyFunc as myfunc from '~/.vim/myvim9.vim'
>
> Import only works in a Vim9 script file. Not that it is impossible in
> legacy Vim script, but I think it's a good idea not to mix a new command
> in an old script file. e.g., do you need to use "s:myfunc" here?
>
> > else
> > function s:myfunc()
> > ...
> > endfunction
> > endif
> > command MyCommand -nargs=0 -bar call s:myfunc()
I wrote this based on my interpretation of doc/vim9.txt lines 435-438:
<quote>
Import in legacy Vim script ~
If an `import` statement is used in legacy Vim script, for identifier the
script-local "s:" namespace will be used, even when "s:" is not specified.
</quote>
and BTW I also assumed that, in the "true" branch of the ":if
exists(':vim9script') == 2" conditional block I could, instead of an
:import statement, use a :def ... enddef for the same local function,
but only with careful writing. Of course the s: namespace is needed in
the command definition, and therefore also in the "else" branch, to
make sure that the same function (which will be forced to script-local
if imported) is defined the fast way if possible, with fallback on the
traditional ":function" definition if necessary.
The idea of mixing old and new code in, for instance, a vimrc is that
I might want to use incompatible features elsewhere in the script, for
instance a :function with the range modifier, or a curly-braces-name.
Or a function with a variable number of arguments if the script might
conceivably be run in a Vim executable not yet supporting Dictionary
variables, but this latter hypothesis is of dwindling likelihood as
time goes on.
Best regards,
Tony.