This PR modifies the object and class variable declarations to require use of the :var keyword.
The current object/class variable declaration syntax is inconsistent with that used in other language contexts where :var
, :const
, or :final
introduce variables. It is also inconsistent with object/class method declarations which reuse the syntax employed for all other function declarations, i.e., :def
.
Classes and interfaces are not special enough, as syntactic structures, to warrant these deviations. Similar programming languages usually reuse their general variable declaration syntax for field declarations, and the small number that don't use a dedicated and internally consistent class syntax.
Aside, from being a needless deviation from the general syntax with no obvious benefit, it also introduces various syntactic oddities, as highlighted by the proposed syntax for final object/class variables.
I initially raised this topic in and around the first call for comments on Vim9 classes, as did others. The last I heard was that it was still "under consideration" but, with the recent unfortunate developments, some of these decisions appear to have been implicitly crystallised without further discussion. As I dislike the syntax as much now, having actually used it anger, as I did when I first saw it twelve months ago it seems worth raising again while there is still an opportunity to change it.
For reference this topic is covered in vim9class.txt under "Rationale" -> "Declaring object and class variables".
Ā Ā https://github.com/vim/vim/pull/13670
(7Ā files)
ā
Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.
@yegappan requested changes on this pull request.
> @@ -156,8 +156,8 @@ On the other hand, if you do not want the object variables to be read directly from outside the class or its sub-classes, you can make them protected. This is done by prefixing an underscore to the name: > - this._lnum: number - this._col number + var_lnum: number + var_col number
A space is needed between var and "_lnum" and "_col".
In src/errors.h:
> @@ -3567,6 +3565,8 @@ EXTERN char e_using_class_as_var_val[] INIT(= N_("E1405: Cannot use a Class as a variable or value")); EXTERN char e_using_typealias_as_var_val[] INIT(= N_("E1406: Cannot use a Typealias as a variable or value")); +EXTERN char e_static_must_be_followed_by_var_or_def[] + INIT(= N_("E1407: Static must be followed by \"var\" or \"def\""));
Can you reuse E1368 instead of introducing a new error number?
In src/testdir/test_vim9_class.vim:
> @@ -74,7 +74,7 @@ def Test_class_basic() this endclass END - v9.CheckSourceFailure(lines, 'E1317: Invalid object variable declaration: this', 3) + v9.CheckSourceFailure(lines, 'E1318: Not a valid command in a class: this', 3)
Can you add a test for using the prefix "variable" instead of "var" for the declaring an object variable?
In src/testdir/test_vim9_class.vim:
> # Use "that" instead of "this" lines =<< trim END vim9script class Something - this.count: number + var count: number
Can you add a test for using the previous syntax using the "this" prefix with object and class variables and check whether they all fail?
> @@ -690,8 +690,8 @@ value for the object variables will be used. This is a more useful example, with default values: > class TextPosition - this.lnum: number = 1 - this.col: number = 1 + var lnum: number = 1 + var col: number = 1 endclass If you want the constructor to have mandatory arguments, you need to write it
The "Declaring object and class variables" section needs to be updated to reflect the rationale for using the "var" prefix now.
ā
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.
I like the rationale for making the variable declaration consistent inside and outside a class.
ā
Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.
I agree. I think consistently requiring the use of :var
makes sense
ā
Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.
@dkearns commented on this pull request.
> @@ -156,8 +156,8 @@ On the other hand, if you do not want the object variables to be read directly from outside the class or its sub-classes, you can make them protected. This is done by prefixing an underscore to the name: > - this._lnum: number - this._col number + var_lnum: number + var_col number
Done
ā
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.
@dkearns commented on this pull request.
In src/errors.h:
> @@ -3567,6 +3565,8 @@ EXTERN char e_using_class_as_var_val[] INIT(= N_("E1405: Cannot use a Class as a variable or value")); EXTERN char e_using_typealias_as_var_val[] INIT(= N_("E1406: Cannot use a Typealias as a variable or value")); +EXTERN char e_static_must_be_followed_by_var_or_def[] + INIT(= N_("E1407: Static must be followed by \"var\" or \"def\""));
Done
ā
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.
@dkearns commented on this pull request.
In src/testdir/test_vim9_class.vim:
> # Use "that" instead of "this" lines =<< trim END vim9script class Something - this.count: number + var count: number
Done
ā
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.
@dkearns commented on this pull request.
> @@ -690,8 +690,8 @@ value for the object variables will be used. This is a more useful example, with default values: > class TextPosition - this.lnum: number = 1 - this.col: number = 1 + var lnum: number = 1 + var col: number = 1 endclass If you want the constructor to have mandatory arguments, you need to write it
Done
ā
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.
great! thank you very muuuuuuuuuuch:
seems you are fixing another it was an odd case: this.
š
// (#11827 (comment))
ā
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.
@yegappan approved this pull request.
ā
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.
thanks for this!
ā
Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.
ā
Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.
vim9cmd var x = 123
in cmdline seems still cannot use 'var', is it designed, or forgot?
ā
Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.
There's a test for that usage, in a script, in test_vim9_cmd.vim where it's allowed but it's intentionally disallowed when called interactively, with a dedicated error message.
ā
Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.
yea, so do you know what's the reason why it was disallowed?
i felt it much useful if adhoc to run something..
ā
Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.
Well, in Vim9 script var
declared variables, outside of functions, are script local. So
vim9cmd var x = 123
doesn't make much sense on the command-line where x would be global. You can declare a global variable using
vim9cmd g:x = 123
ā
Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.
@Shane-XB-Qian, it looks like you've been here before. š
ā
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.
ā
Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.
oh, one reason i can image perhaps because : to vim9, if 'var t:x' then x would a be type name.
if such, then let it be, how do you think?
ā
Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.
but here it is 't:x' not 't: x', so i have no idea why..
ā
Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.
let's just leave it as it currently is.
ā
Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.
I thought it was a bit weird when I first saw it as well. If you think of them more as explicit global dictionaries, as they're presented via get()
, it's easier to understand.
We could disallow type inference for these but then you have optional var
(as the current mechanism has to be supported) and disallowed type inference.
Even if it was going to be supported it could be done in the future without any compatibility concerns.
ā
Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.
ā
Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.
Yes, the error is confusing. Is "E1079: Cannot declare a script-local variable on the command line" good enough?
ā
Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.
ā
Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.