[vim9] vim9 function not found in viml script

131 views
Skip to first unread message

hebar...@googlemail.com

unread,
Aug 23, 2022, 12:18:35 PM8/23/22
to vim_dev
The below script, when executed with "so %", gives error "E117: unknown function III".
Is there any way to remedy this (given that the same script, running as "vim9script" runs w/o error)?
As a side note: the functions below are part of my .vimrc; so I'm having trouble flagging the whole file as "vim9script"...
=======================
def III(hx: string): string
   return hx
enddef

def DDD(hx: string): string
   return hx
enddef

def SSS()
   echo III("HALA")
   echo DDD("HULU")
enddef

call SSS()

=======================

Yegappan Lakshmanan

unread,
Aug 23, 2022, 12:25:51 PM8/23/22
to vim_dev
Hi,

You need to prefix the command with ":vim9cmd"

- Yegappan
> --
> --
> You received this message from the "vim_dev" 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
>
> ---
> You received this message because you are subscribed to the Google Groups "vim_dev" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+u...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/vim_dev/5d99e83d-eef6-40c0-b878-03d2012b638an%40googlegroups.com.

hebar...@googlemail.com

unread,
Aug 23, 2022, 1:05:54 PM8/23/22
to vim_dev
@yegappanl

The only command I see I can prefix with "vim9cmd" is "call SSS()" (the others already are vim9 functions), to become "vim9cmd SSS()".
This however gives me error "E117: unknown function SSS" (instead of "III").

Prefixing the vim9(!) function III with "vim9cmd" actually works but makes - from my point of view - no sense at all (why would I need to flag a vim9 function as vim9 function if that's clear from its definition?)

Can you (or anybody) shed some light on this?

Info: I'm using the 9.0.242

hebar...@googlemail.com

unread,
Aug 23, 2022, 1:09:30 PM8/23/22
to vim_dev
Addendum: Actucally, both functions, DDD and III need to be flagged as "vim9cmd".

On the other hand, there are functions in the same script defined with "def" that work with no problems...

Yegappan Lakshmanan

unread,
Aug 23, 2022, 1:48:18 PM8/23/22
to vim_dev
Hi,

On Tue, Aug 23, 2022 at 10:05 AM 'hebar...@googlemail.com' via vim_dev
<vim...@googlegroups.com> wrote:
>
> @yegappanl
>
> The only command I see I can prefix with "vim9cmd" is "call SSS()" (the others already are vim9 functions), to become "vim9cmd SSS()".
> This however gives me error "E117: unknown function SSS" (instead of "III").
>

I was not clear in my earlier reply. You need to prefix the "source"
command with
"vim9cmd" and not the functions defined in the file.

When Vim sources a file, it needs a way to determine whether to parse
the file as
a legacy Vim script or as a Vim9 script. If the first command in the
file is "vim9script",
then Vim parses the file as a Vim 9 script. Otherwise it parses the
file as a legacy script.
In your case, since the file doesn't start with the vim9script
command, you need to
use the ":vim9cmd" prefix to the "source" command to force Vim to
parse the file as
a Vim9 script.

- Yegappan
> To view this discussion on the web visit https://groups.google.com/d/msgid/vim_dev/b95eb6c6-f509-44ee-a1ac-a209306a52dfn%40googlegroups.com.

Bram Moolenaar

unread,
Aug 23, 2022, 2:55:26 PM8/23/22
to vim...@googlegroups.com
It would work best to put "vim9script" in the first line. But if you
don't want that, prefix "g:" to the function names in SSS(). Since this
is a legacy script, functions are defined in the global namespace by
default. But in a :def function prefixing "g:" is required for using a
global function.

--
BROTHER MAYNARD: Armaments Chapter Two Verses Nine to Twenty One.
ANOTHER MONK: And St. Attila raised his hand grenade up on high saying "O
Lord bless this thy hand grenade that with it thou mayest
blow thine enemies to tiny bits, in thy mercy. "and the Lord
did grin and people did feast upon the lambs and sloths and
carp and anchovies and orang-utans and breakfast cereals and
fruit bats and...
BROTHER MAYNARD: Skip a bit brother ...
"Monty Python and the Holy Grail" PYTHON (MONTY) PICTURES LTD

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// \\\
\\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///
Reply all
Reply to author
Forward
0 new messages