Mercurial > vim
diff runtime/doc/vim9class.txt @ 33942:3bba09502b8d v9.0.2167
patch 9.0.2167: Vim9: not consistently using :var for declarations
Commit: https://github.com/vim/vim/commit/74da0ee0a24799a312a3a8a65858237185ef7a23
Author: Doug Kearns <dougkearns@gmail.com>
Date: Thu Dec 14 20:26:26 2023 +0100
patch 9.0.2167: Vim9: not consistently using :var for declarations
Problem: Vim9-script object/class variable declarations use syntax
that is inconsistent with the rest of the language.
Solution: Use :var to declare object and class variables.
closes: #13670
Signed-off-by: Doug Kearns <dougkearns@gmail.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
author | Christian Brabandt <cb@256bit.org> |
---|---|
date | Thu, 14 Dec 2023 20:30:06 +0100 |
parents | 050160b94f02 |
children | 27746ed6cb05 |
line wrap: on
line diff
--- a/runtime/doc/vim9class.txt +++ b/runtime/doc/vim9class.txt @@ -78,8 +78,8 @@ Let's start with a simple example: a cla below for how to do this more efficiently): > class TextPosition - this.lnum: number - this.col: number + var lnum: number + var col: number def new(lnum: number, col: number) this.lnum = lnum @@ -156,8 +156,8 @@ On the other hand, if you do not want th 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 Now you need to provide methods to get the value of the protected variables. These are commonly called getters. We recommend using a name that starts with @@ -209,8 +209,8 @@ Many constructors take values for the ob see this pattern: > class SomeClass - this.lnum: number - this.col: number + var lnum: number + var col: number def new(lnum: number, col: number) this.lnum = lnum @@ -235,8 +235,8 @@ Putting together this way of using new() results in a much shorter class definition than what we started with: > class TextPosition - public this.lnum: number - public this.col: number + public var lnum: number + public var col: number def new(this.lnum, this.col) enddef @@ -277,8 +277,8 @@ Class members are declared with "static" prefix in the class where they are defined: > class OtherThing - this.size: number - static totalSize: number + var size: number + static var totalSize: number def new(this.size) totalSize += this.size @@ -297,9 +297,9 @@ underscore as the first character in the prefixing "public": > class OtherThing - static total: number # anybody can read, only class can write - static _sum: number # only class can read and write - public static result: number # anybody can read and write + static var total: number # anybody can read, only class can write + static var _sum: number # only class can read and write + public static var result: number # anybody can read and write endclass < *class-method* @@ -308,8 +308,8 @@ variables but they have no access to the "this" keyword: > class OtherThing - this.size: number - static totalSize: number + var size: number + static var totalSize: number # Clear the total size and return the value it had before. static def ClearTotalSize(): number @@ -345,14 +345,14 @@ outside of the defining class: > vim9script class Vehicle - static nextID: number = 1000 + static var nextID: number = 1000 static def GetID(): number nextID += 1 return nextID enddef endclass class Car extends Vehicle - this.myID: number + var myID: number def new() this.myID = Vehicle.GetID() enddef @@ -380,20 +380,20 @@ it is. The Shape class functions as the class, for which objects can be created. Example: > abstract class Shape - this.color = Color.Black - this.thickness = 10 + var color = Color.Black + var thickness = 10 endclass class Square extends Shape - this.size: number + var size: number def new(this.size) enddef endclass class Triangle extends Shape - this.base: number - this.height: number + var base: number + var height: number def new(this.base, this.height) enddef @@ -430,8 +430,8 @@ interface called HasSurface, which speci a number. This example extends the one above: > abstract class Shape - this.color = Color.Black - this.thickness = 10 + var color = Color.Black + var thickness = 10 endclass interface HasSurface @@ -439,7 +439,7 @@ a number. This example extends the one endinterface class Square extends Shape implements HasSurface - this.size: number + var size: number def new(this.size) enddef @@ -450,8 +450,8 @@ a number. This example extends the one endclass class Triangle extends Shape implements HasSurface - this.base: number - this.height: number + var base: number + var height: number def new(this.base, this.height) enddef @@ -598,13 +598,13 @@ Items in a class ~ *E1318* *E1325* *E1388* Inside a class, in between `:class` and `:endclass`, these items can appear: - An object variable declaration: > - this._protectedVariableName: memberType - this.readonlyVariableName: memberType - public this.readwriteVariableName: memberType + var _protectedVariableName: memberType + var readonlyVariableName: memberType + public var readwriteVariableName: memberType - A class variable declaration: > - static _protectedClassVariableName: memberType - static readonlyClassVariableName: memberType - static public readwriteClassVariableName: memberType + static var _protectedClassVariableName: memberType + static var readonlyClassVariableName: memberType + static var public readwriteClassVariableName: memberType - A constructor method: > def new(arguments) def newName(arguments) @@ -620,9 +620,9 @@ this explicitly with ": {type}". For si initializer, such as "= 123", and Vim will see that the type is a number. Avoid doing this for more complex types and when the type will be incomplete. For example: > - this.nameList = [] + var nameList = [] This specifies a list, but the item type is unknown. Better use: > - this.nameList: list<string> + var nameList: list<string> The initialization isn't needed, the list is empty by default. *E1330* Some types cannot be used, such as "void", "null" and "v:none". @@ -646,7 +646,7 @@ An interface can declare methods with `: return type, but without the body and without `:enddef`. Example: > interface HasSurface - this.size: number + var size: number def Surface(): number endinterface @@ -674,9 +674,9 @@ defined. This default constructor will variables, in the order they were specified. Thus if your class looks like: > class AutoNew - this.name: string - this.age: number - this.gender: Gender + var name: string + var age: number + var gender: Gender endclass Then the default constructor will be: > @@ -690,8 +690,8 @@ value for the object variables will be u 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 @@ -947,26 +947,26 @@ Following that Vim object variables coul Some users pointed out that this looks more like an assignment than a declaration. Adding "var" changes that: > class Point - var this.x: number - var this.y = 0 + var x: number + var y = 0 endclass We also need to be able to declare class variables using the "static" keyword. There we can also choose to leave out "var": > class Point - var this.x: number + var x: number static count = 0 endclass Or do use it, before "static": > class Point - var this.x: number + var x: number var static count = 0 endclass Or after "static": > class Point - var this.x: number + var x: number static var count = 0 endclass @@ -974,9 +974,16 @@ This is more in line with "static def Fu There is no clear preference whether to use "var" or not. The two main reasons to leave it out are: -1. TypeScript, Java and other popular languages do not use it. +1. TypeScript and other popular languages do not use it. 2. Less clutter. +However, it is more common for languages to reuse their general variable and +function declaration syntax for class/object variables and methods. Vim9 also +reuses the general function declaration syntax for methods. So, for the sake +of consistency, we require "var" in these declarations. + +This also allows for a natural use of "final" and "const" in the future. + Using "ClassName.new()" to construct an object ~