Wednesday, July 7, 2021

Re: Help converting function to Vim9 script

> I try not to use global variables within functions. Is there an
> alternative enabling non-use of global variables?

Yes, use block-local, function-local, or script-local variables.  Those don't need any explicit scope.

> Also, I thought the new Vim9 Vimscript was getting rid of the need for
> g: and l: and a: etc. That stuff used to kill my brain.

`l:` and `a:` are invalid now.  You don't need them anymore.

`s:` is still valid, but in practice, you never need it.

`v:` is no longer needed either in front of some common values: `true`, `false`, `null`.

As for `g:`, how else would you tell Vim that the variable is global and not local to the current script/function/block?  Same thing for buffer-local, window-local and tab-local variables.  `b:`, `w:`, `t:` might look weird, but we need a way to specify in which namespace such variable names should be looked for.  There is no equivalent in other programming languages, because there is no concept of "current buffer", "current window", "current tab page"... in them.  Anyway, in practice, you rarely need those scopes.  The vast majority of your variables are script-local, function-local, or block-local.  And for all of those, no explicit scope is required any longer.

On Sunday, July 4, 2021 at 11:52:29 PM UTC+2 stevelitt wrote:
Bram Moolenaar said on Sun, 04 Jul 2021 23:30:01 +0200


>Well, this works:
>
>def Fix_beginfigs()
> g:index = 1
> g/^beginfig(\d*);$/s//\='beginfig(' .. g:index .. ');'/ | g:index =
> g:index + 1 unlet g:index
>enddef
>
>However, using a legacy function would not be worse. The global
>command does most of the work, what function it's in doesn't really
>matter.

Hi Bram,

I try not to use global variables within functions. Is there an
alternative enabling non-use of global variables?

Also, I thought the new Vim9 Vimscript was getting rid of the need for
g: and l: and a: etc. That stuff used to kill my brain.

Thanks,

SteveT

Steve Litt
Spring 2021 featured book: Troubleshooting Techniques of the Successful
Technologist http://www.troubleshooters.com/techniques

--
--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/3c5d4bab-c66a-47f9-93fc-74ccf3217133n%40googlegroups.com.

No comments:

Post a Comment