Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork5.9k
Add :const for safer scripting#4541
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
brammool commentedJun 15, 2019
Thanks for doing this. I was actually thinking about this, since I was working with Javascript. And the ":const" command was available. |
k-takata commentedJun 15, 2019
Fixed by8.1.1539. |
rhysd commentedJun 16, 2019
Thank you for your quick review and merge! |
brammool commentedJun 16, 2019 via email
@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 (Kenclosed 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 /// |
rhysd commentedJun 16, 2019
@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
:constcommand 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
iis intended not to be modified,):lockvaris effective:However, using
:letand:lockvarhas following downsides::lockvarat everywhere it is required:letand:lockvarmay not be efficient because it introduces overhead of one more command.Solution
In JavaScript,
constis provided for defining a variable.:constdoes the similar thing in Vim script. With it, above example can be written as follows:Here
:constis used instead of:let.:constinternally locks the variablei. Soiis no longer modifiable. And it is more efficient than using both:letand:lockvarbecause both are done in one command.In addition,
:constraises an error if the variable is already existing:So
:constdoes not lock and overwrite existing variable unexpectedly andxis guaranteed to be a new variable.Another use case of
:constwould be defining constants in the script. For example:Here the constant is not intended to be modified any more. So
:constis better than:let.