Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
/vimPublic

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

Closed
rhysd wants to merge4 commits intovim:masterfromrhysd:const
Closed

Add :const for safer scripting#4541

rhysd wants to merge4 commits intovim:masterfromrhysd:const

Conversation

@rhysd
Copy link
Contributor

Hi all,

This PR has proposed adding:const command to Vim.

Problem

Scope of variables in Vim script function is very dynamic.

leti=0if some_condition" In heavily nested or big statements...leti=1" Unexpectedly using the same name variableendifechoi" => 1

This sometimes causes a bug by modifying variables unexpectedly. To prevent this, (ifi is intended not to be modified,):lockvar is effective:

leti=0lockvariif some_conditionleti=1" Errorendifechoi

However, using:let and:lockvar has following downsides:

  • it is hard to give:lockvar at everywhere it is required
  • using: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.

constnumber=42;number=99;// TypeError: invalid assignment to const `number'

:const does the similar thing in Vim script. With it, above example can be written as follows:

consti=0if some_conditionleti=1" E995endifechoi" => 1

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:

letx=0constx=1" => E995

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:

consts:SEP=has('win32') ?'\' :'/'

Here the constant is not intended to be modified any more. So:const is better than:let.

tyru and yutkat reacted with thumbs up emoji
@brammool
Copy link
Contributor

Thanks for doing this. I was actually thinking about this, since I was working with Javascript. And the ":const" command was available.

rhysd reacted with heart emoji

@k-takata
Copy link
Member

Fixed by8.1.1539.

@rhysd
Copy link
ContributorAuthor

@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 atruntime/doc/version8.txt in your mind?

@rhysdrhysd mentioned this pull requestJun 16, 2019
@brammool
Copy link
Contributor

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
Copy link
ContributorAuthor

Will do. I didn't see your name on github.

@brammool Thank you for your kind consideration. And I'm sorry that I forgot to show my name in advance.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

3 participants

@rhysd@brammool@k-takata

[8]ページ先頭

©2009-2025 Movatter.jp