-
-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Allow assigning a Funcref to autoload variable #11031
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow assigning a Funcref to autoload variable #11031
Conversation
Codecov Report
@@ Coverage Diff @@
## master #11031 +/- ##
==========================================
- Coverage 82.47% 82.39% -0.08%
==========================================
Files 152 152
Lines 177682 177683 +1
Branches 40341 40341
==========================================
- Hits 146539 146410 -129
- Misses 18948 19049 +101
- Partials 12195 12224 +29
Flags with carried forward coverage won't be shown. Click here to find out more.
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. |
|
In Vim, defining a User function that starts with a lowercase letter
is not allowed because it could overwrite a built-in function.
However, defining an autoload function that starts with a lowercase
letter is permitted because it does not cause the above problem.
For the same reason, Vim does not allow Funcref to be assigned to a
global variable whose name begins with a lowercase letter.
This also applies to the autoload variable; `let foo#tr =
function('tr')` is not allowed and must be `let Foo#tr =
function('tr')`.
I see no need for this restriction. `let foo#tr =
function('tr')` should be allowed.
Well, not when used with the "name#" prefix. But inside the autoload
script it can be used without the prefix, which does cause shadowing a
builtin function.
Thus when you, like in the test, do:
let foo#tr = function('tr')
Then in the "autoload/foo.vim" script calling tr() does have the
problem. At least in Vim9 script, where the "s:" prefix is not used.
Right?
…--
This computer is so slow, it takes forever to execute and endless loop!
/// Bram Moolenaar -- ***@***.*** -- http://www.Moolenaar.net \\\
/// \\\
\\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///
|
|
In legacy Vim script, " autoload/name.vim
echo foo " This is g:foo, not name#foo
echo s:foo " This is s:foo, not name#foo
function name#func()
echo foo " This is l:foo, not name#foo
endfunctionIn Vim9 script, as you say, exported variable is an autoload variable and can be access without prefix. vim9script
# autoload/name.vim
# This occurs an error "E704: Funcref variable name must start with a capital: tr"
export var tr = () => 'tr'vim9script
# autoload/name.vim
# This variable can rewrite by `let name#tr = { -> 'tr' }` from outside of script with this patch.
export var tr = ''
def Foo()
# But this `tr()` is always built-in function. Vim9 script is compiled first.
echo tr('foo', 'o', 'a')
enddef |
|
In legacy Vim script, `name#` prefix is always necessary.
```vim
" autoload/name.vim
echo foo " This is g:foo, not name#foo
echo s:foo " This is s:foo, not name#foo
function name#func()
echo foo " This is l:foo, not name#foo
endfunction
```
OK, and it actually checks if the name of the script starts with a
capital, not the variable itself, so that is pointless.
In Vim9 script, as you say, exported variable is an autoload variable
and can be access without prefix.
But there is no problem.
```vim
vim9script
# autoload/name.vim
# This occurs an error "E704: Funcref variable name must start with a capital: tr"
export var tr = () => 'tr'
```
```vim
vim9script
# autoload/name.vim
# This variable can rewrite by `let name#tr = { -> 'tr' }` from outside of script with this patch.
export var tr = ''
def Foo()
# But this `tr()` is always built-in function. Vim9 script is compiled first.
echo tr('foo', 'o', 'a')
enddef
```
Hmm, I cannot think of a way to go around this, assigning a variable
outside of a Vim9 script can be expected to always fail.
…--
BEDEVERE: Wait. Wait ... tell me, what also floats on water?
ALL: Bread? No, no, no. Apples .... gravy ... very small rocks ...
ARTHUR: A duck.
"Monty Python and the Holy Grail" PYTHON (MONTY) PICTURES LTD
/// Bram Moolenaar -- ***@***.*** -- http://www.Moolenaar.net \\\
/// \\\
\\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///
|
Problem: Check for uppercase char in autoload name is wrong, it checks the
name of the script.
Solution: Remove the check. (closes vim/vim#11031)
vim/vim@6c667bd
Co-authored-by: thinca <thinca@gmail.com>
Problem: Check for uppercase char in autoload name is wrong, it checks the
name of the script.
Solution: Remove the check. (closes vim/vim#11031)
vim/vim@6c667bd
Co-authored-by: thinca <thinca@gmail.com>
In Vim, defining a User function that starts with a lowercase letter is not allowed because it could overwrite a built-in function.
However, defining an autoload function that starts with a lowercase letter is permitted because it does not cause the above problem.
For the same reason, Vim does not allow Funcref to be assigned to a global variable whose name begins with a lowercase letter.
This also applies to the autoload variable;
let foo#tr = function('tr')is not allowed and must belet Foo#tr = function('tr').I see no need for this restriction.
let foo#tr = function('tr')should be allowed.