1

Looking at vim's help (i.e. :he exists()) it should be possible to use script variables. e.g.

vim9script def Bar() echom "Bar called" enddef var script_variable: string if exists('script_variable') Bar() endif 

However, it seems that any code in the script should already know whether script_variable was declared or not. i.e. testing with exists('script_variable') is completely useless.

Declaring script_variable conditionally, e.g. inside an if/endif block or from within a function def/enddef block, will limit its scope within that block and hence the testing of its existance from outside those blocks will always fail. Have I missed something?

2 Answers 2

2

We are allowed to use vim9script def in vimscript, so technically it is possible to have a script local var conditionally declared in vimscript and used in vim9script def:

if 0 let s:test = "hello world" else unlet s:test endif def Test() if exists("test") echo "test exists:" s:test else echo "test doesn't exist" endif enddef augroup test au! au BufEnter * call Test() augroup END 

Alghouth def can see it as test in exists(), it can't use it as test, I had to add s: -- echo s:test

2
  • Thanks for your answer. Please let me clarify my understanding before accepting this as the answer. Do you mean to say it is useful only in mixed vimscript and vim9script scenario? i.e. in a vim9script only case it is not useful? Commented Aug 21 at 4:51
  • I don't know for sure, but I believe that it is only useful in a mixed context scenario. For pure vim9script it doesn't make sense. Commented Aug 21 at 6:16
1

Let's take a step back. Nowhere in :help exists() does it explicitly say you can check a script-variable. The section points to :help internal-variables where we learn that they come in many flavors, one of them being a script-variable. So, yes, you are correct. Although the use case in the question seems to be neither intended nor anticipated. I'd guess what you ask for works mostly for reasons of consistency—or: by accident—if you prefer to put it that way.

Rather than asking "what's the use of this edge case?" we should accept this as a necessary side-effect of language design. Any language with powerful-enough expressiveness will allow the creation of nonsensical statements. They are easily made up; take :if true—yet nothing keeps me from littering my code with it.

The alternative would be to add a test for a script-variable to the implementation of exists() which will:

  • increase complexity
  • reduce consistency
  • prevent the check in Maxim's answer

To sum it up: it will hurt Vim maintainers and script authors alike. The ability to write something that does not really make sense most of the time seems like a small price to pay in comparison.


As a side note, the ability to create nonsense isn't restricted to artificial languages. Natural languages can be used to create nonsense as well, as evidenced by the Jaberwocky or, well, most political slogans.

1
  • 1
    Hi, thanks for providing additional info. Just to provide some context, I had come across the use of if exists('s:script_variable_name') idiom in some of the legacy scripts that I'm converting to vim9script as a way of learning by practicing. That is, it may be a nonsensical use in a vim9script now, it wasn't so in a legacy vimscript. Commented Aug 21 at 23:11

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.