Vim9 - Can not get compile to recognize a deep variable

22 views
Skip to first unread message

Chainsaw

unread,
Feb 16, 2022, 4:20:47 PM2/16/22
to vim...@googlegroups.com
Hello,

I have read through the Vim9.txt, and can not find a solution to this issue.
When I call Parse(), the script version with the entire command on one line (|) gives error 'E121: Undefined variable: udp' in function add().
Both commands work if used on the command line, so its particular to running in a Vim9 script.

This works:
def g:Parse(): void
var udp: list<string>

:3global/^\s*<per/substitute/^\s*<per\(.\{-}"\)\{2}\|\s\+>.*$//g
:3substitute/\s\+.\{-}"\zs.\{-}\ze"/\=add(udp, submatch(0))/gn
# :3global/^\s*<per/substitute/^\s*<per\(.\{-}"\)\{2}\|\s\+>.*$//g
# | substitute/\s\+.\{-}"\zs.\{-}\ze"/\=add(udp, submatch(0))/gn

echo udp
enddef

This does not work:
def g:Parse(): void
var udp: list<string>

# :3global/^\s*<per/substitute/^\s*<per\(.\{-}"\)\{2}\|\s\+>.*$//g
# :3substitute/\s\+.\{-}"\zs.\{-}\ze"/\=add(udp, submatch(0))/gn
:3global/^\s*<per/substitute/^\s*<per\(.\{-}"\)\{2}\|\s\+>.*$//g
| substitute/\s\+.\{-}"\zs.\{-}\ze"/\=add(udp, submatch(0))/gn

echo udp
enddef

Thanks,
Chainsaw

Bram Moolenaar

unread,
Feb 16, 2022, 5:16:40 PM2/16/22
to vim...@googlegroups.com, Chainsaw
This happens because when compiling the function, when the :substitute
command is separate, the compiler recognizes the "\=" in the
substitution. Then it works.

When joining the two lines the compiler only sees the ":global" command
and doesn't check for the "\=" argument.

This is nearly impossible to fix, because parsing the ":global" command
requires parsing just about any Ex command. That can only be done at
runtime, and then the local variable is not visible.

You'll have to use a workaround. You could make the "udp" variable a
global. That is what would happen in legacy script as well.

--
ERROR 047: Keyboard not found. Press RETURN to continue.

/// 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 ///

Chainsaw

unread,
Feb 16, 2022, 6:46:28 PM2/16/22
to vim...@googlegroups.com
Bram,

First, thank you for Vim. What you have created is amazing, and has literally
filled every void I had in my workflow, from text editing, to writing software
to streamline specific tasks, and infinitely more. The only thing I don't like
is I use Vim so much, that I find myself typing a ':' everywhere, on desktop,
in title bar of browser, and I tried to save a file today in Word by typing ':w'.

I put the 'udp' declaration outside of the function and made global. That worked,
thank you. However, the declaration 'g:udp: list<string>' gives an E488 error.
So, I tried 'g:udp = []' and that worked. Can you please explain the proper way
to declare the list globally, because I thought the first way was correct.

Thank you,
Samson




bfrg

unread,
Feb 16, 2022, 9:47:45 PM2/16/22
to vim...@googlegroups.com
Samson,

global, buffer, window and tabpage variables do not have a specific type. This
is explained under ":help variable-types".

Ben
> --
> --
> You received this message from the "vim_use" 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_use" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+u...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/20220216182719.00003249%40cfl.rr.com.

Chainsaw

unread,
Feb 16, 2022, 10:57:14 PM2/16/22
to vim...@googlegroups.com
Ben,

Thanks for pinpoint information. I read that section earlier today and
overlooked the Global in that sentence.

Like 'var udp: list<string>', it would be nice to declare the global equivalent
'g:udp: list<any>' (redundant, but reminds me what Vim is expecting when I look
at the code).

Thanks again,
Samson

Bram Moolenaar

unread,
Feb 17, 2022, 5:59:23 AM2/17/22
to vim...@googlegroups.com, Chainsaw

> Like 'var udp: list<string>', it would be nice to declare the global
> equivalent 'g:udp: list<any>' (redundant, but reminds me what Vim is
> expecting when I look at the code).

The problem is that the type can't be enforced. In legacy script it has
always been possible to assign anything to a global variable. Also it
can be deleted with :unlet and given a completely different value.

In most cases you can use a script-local variable. In Vim9 script you
can give these a type and that will be enforced. This works best,
because references to the variable will be in the same script, the
knowledge of what the variable is for and what it's type is is
localized.

Theoretically it would be possible to add a new type of global variable
that works differently. I do not see enough need for this, while it
does add complexity. And especially that when using g:someVar you don't
know where it was defined and what kind of variable it is.

--
hundred-and-one symptoms of being an internet addict:
56. You leave the modem speaker on after connecting because you think it
sounds like the ocean wind...the perfect soundtrack for "surfing the net".

Chainsaw

unread,
Feb 17, 2022, 9:00:34 AM2/17/22
to vim...@googlegroups.com
> In most cases you can use a script-local variable. In Vim9 script you
> can give these a type and that will be enforced. This works best,
> because references to the variable will be in the same script, the
> knowledge of what the variable is for and what it's type is is
> localized.

Exactly how I approach every new function, global is my last resort.

Thanks for your replies.
Samson

JB

unread,
Feb 17, 2022, 5:31:06 PM2/17/22
to vim...@googlegroups.com
------- Original Message -------

On Wednesday, February 16th, 2022 at 3:27 PM, Chainsaw <chains...@cfl.rr.com> wrote:

> Bram,
>
> First, thank you for Vim. What you have created is amazing, and has literally filled every void I had in my workflow, from text editing, to writing software to streamline specific tasks, and infinitely more. The only thing I don't like is I use Vim so much, that I find myself typing a ':' everywhere, on desktop, in title bar of browser, and I tried to save a file today in Word by typing ':w'.

I do this, too. I'm so accustomed to using Vim's shortcuts that I had to install Pentadactyl and its forks on my web browsers. It's the worst addiction; it's the best addiction.
Reply all
Reply to author
Forward
0 new messages