vim9script
def F1()
export const val1 = 'foo1'
enddef
def F2(): dict<any>
return {k: val1}
enddef
def F3()
F1()
F2()
enddef
F3()
produces
Error detected while compiling :source buffer=1[16]..function <SNR>38_F3[2]..<SNR>38_F2:
line 1:
E1001: Variable not found: val1
The following with a twist works
vim9script
def F1()
export const val1 = 'foo1'
enddef
def F2(): dict<any>
return {k: val1}
enddef
F1() # <<<<<<<<<<<<<<<<<<<< execute at script level
def F3()
F2()
enddef
F3()
Something else that works is putting F2()
in a different file, importing that file into /tmp/x.vim
, and then running F2
/tmp/x.vim
vim9script
import autoload '/tmp/y.vim'
def F1()
export const val1 = 'foo1'
enddef
def F3()
F1()
echo y.F2()
enddef
F3()
/tmp/y.vim
vim9script
import autoload '/tmp/x.vim'
def F2(): dict<any>
return {k: x.val1}
enddef
I'm refactoring some code written over two years ago when vim9script
was first becoming available. I ran into this in the first file that gets run when a command defined in a small plugin
is executed. The code in question actually looks like
def InitHighlights()
export const hl_label = settings.Setting('hl_label')
...
And some initialization has to be done before the const
are defined by the InitHighlights
function. It does seem pretty weird, and I was probably surprised when it worked.
If doing export const
in a function is made an error, I can probably easily handle it by putting the const stuff in a different file and be careful that that file doesn't get imported/accessed until some initialization is done. Or maybe do the exports in the file that does the initialization.
Seems it should either work or fail.
9.1.516
ubuntu/gtk3
No response
—
Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.
I'm thinking that export const ...
should not be allowed in a def and should generate an error.
On a side issue
Here's one that is not directly related, but shows an example of the difficulty during startup.
This works
var errors: list<string> = settings.InitSettings()
splice.SpliceInit9(errors)
This fails
splice.SpliceInit9(settings.InitSettings())
Probably a compiler bug. I'm guessing the compiler loads splice.vim
before executing settings.InitSettings()
. But the bootstrap code requires settings be initialized before splice is loaded.
—
Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.
Probably a compiler bug. I'm guessing the compiler loads
splice.vim
before executingsettings.InitSettings()
. But the bootstrap code requires settings be initialized before splice is loaded.
Opened #15137 for this.
—
Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.