-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
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
Add :const for safer scripting #4541
Conversation
Thanks for doing this. I was actually thinking about this, since I was working with Javascript. And the ":const" command was available. |
Fixed by 8.1.1539. |
Thank you for your quick review and merge! |
@brammool
Thank you for your quick review and merge!
My name (Ryuichi Hayashida) seems missing in the patch 8.1.1539. Would
you like to keep adding it at Vim 8.2 release note at
`runtime/doc/version8.txt` in your mind?
Will do. I didn't see your name on github.
I'll also reference the pull request, somehow that was missing (Ken
closed the pull request manually).
…--
hundred-and-one symptoms of being an internet addict:
200. You really believe in the concept of a "paperless" office.
/// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\
/// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\ an exciting new programming language -- http://www.Zimbu.org ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///
|
@brammool Thank you for your kind consideration. And I'm sorry that I forgot to show my name in advance. |
Hi all,
This PR has proposed adding
:const
command to Vim.Problem
Scope of variables in Vim script function is very dynamic.
This sometimes causes a bug by modifying variables unexpectedly. To prevent this, (if
i
is intended not to be modified,):lockvar
is effective:However, using
:let
and:lockvar
has following downsides::lockvar
at everywhere it is required:let
and:lockvar
may not be efficient because it introduces overhead of one more command.Solution
In JavaScript,
const
is provided for defining a variable.:const
does the similar thing in Vim script. With it, above example can be written as follows:Here
:const
is used instead of:let
.:const
internally locks the variablei
. Soi
is no longer modifiable. And it is more efficient than using both:let
and:lockvar
because both are done in one command.In addition,
:const
raises an error if the variable is already existing:So
:const
does not lock and overwrite existing variable unexpectedly andx
is guaranteed to be a new variable.Another use case of
:const
would be defining constants in the script. For example:Here the constant is not intended to be modified any more. So
:const
is better than:let
.