Yegappan Lakshmanan | d3eae7b | 2024-03-03 16:26:58 +0100 | [diff] [blame] | 1 | *vim9class.txt* For Vim version 9.1. Last change: 2024 Mar 03 |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 2 | |
| 3 | |
| 4 | VIM REFERENCE MANUAL by Bram Moolenaar |
| 5 | |
| 6 | |
Yegappan Lakshmanan | 49cdd62 | 2023-12-24 11:01:23 +0100 | [diff] [blame] | 7 | Vim9 classes, objects, interfaces, types and enums. *vim9-class* |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 8 | |
| 9 | 1. Overview |Vim9-class-overview| |
| 10 | 2. A simple class |Vim9-simple-class| |
Yegappan Lakshmanan | 00cd182 | 2023-09-18 19:56:49 +0200 | [diff] [blame] | 11 | 3. Class variables and methods |Vim9-class-member| |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 12 | 4. Using an abstract class |Vim9-abstract-class| |
| 13 | 5. Using an interface |Vim9-using-interface| |
| 14 | 6. More class details |Vim9-class| |
| 15 | 7. Type definition |Vim9-type| |
| 16 | 8. Enum |Vim9-enum| |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 17 | |
| 18 | 9. Rationale |
| 19 | 10. To be done later |
| 20 | |
| 21 | ============================================================================== |
| 22 | |
| 23 | 1. Overview *Vim9-class-overview* |
| 24 | |
| 25 | The fancy term is "object-oriented programming". You can find lots of study |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 26 | material on this subject. Here we document what |Vim9| script provides, |
| 27 | assuming you know the basics already. Added are helpful hints about how to |
Yegappan Lakshmanan | 0ab500d | 2023-10-21 11:59:42 +0200 | [diff] [blame] | 28 | use this functionality effectively. Vim9 classes and objects cannot be used |
| 29 | in legacy Vim scripts and legacy functions. |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 30 | |
| 31 | The basic item is an object: |
| 32 | - An object stores state. It contains one or more variables that can each |
| 33 | have a value. |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 34 | - An object provides functions that use and manipulate its state. These |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 35 | functions are invoked "on the object", which is what sets it apart from the |
| 36 | traditional separation of data and code that manipulates the data. |
| 37 | - An object has a well defined interface, with typed member variables and |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 38 | methods. |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 39 | - Objects are created from a class and all objects have the same interface. |
| 40 | This does not change at runtime, it is not dynamic. |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 41 | |
| 42 | An object can only be created by a class. A class provides: |
| 43 | - A new() method, the constructor, which returns an object for the class. |
| 44 | This method is invoked on the class name: MyClass.new(). |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 45 | - State shared by all objects of the class: class variables (class members). |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 46 | - A hierarchy of classes, with super-classes and sub-classes, inheritance. |
| 47 | |
| 48 | An interface is used to specify properties of an object: |
| 49 | - An object can declare several interfaces that it implements. |
| 50 | - Different objects implementing the same interface can be used the same way. |
| 51 | |
| 52 | The class hierarchy allows for single inheritance. Otherwise interfaces are |
| 53 | to be used where needed. |
| 54 | |
| 55 | |
| 56 | Class modeling ~ |
| 57 | |
| 58 | You can model classes any way you like. Keep in mind what you are building, |
| 59 | don't try to model the real world. This can be confusing, especially because |
| 60 | teachers use real-world objects to explain class relations and you might think |
| 61 | your model should therefore reflect the real world. It doesn't! The model |
| 62 | should match your purpose. |
| 63 | |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 64 | Keep in mind that composition (an object contains other objects) is often |
| 65 | better than inheritance (an object extends another object). Don't waste time |
| 66 | trying to find the optimal class model. Or waste time discussing whether a |
| 67 | square is a rectangle or that a rectangle is a square. It doesn't matter. |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 68 | |
| 69 | |
| 70 | ============================================================================== |
| 71 | |
| 72 | 2. A simple class *Vim9-simple-class* |
| 73 | |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 74 | Let's start with a simple example: a class that stores a text position (see |
| 75 | below for how to do this more efficiently): > |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 76 | |
| 77 | class TextPosition |
Doug Kearns | 74da0ee | 2023-12-14 20:26:26 +0100 | [diff] [blame] | 78 | var lnum: number |
| 79 | var col: number |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 80 | |
| 81 | def new(lnum: number, col: number) |
| 82 | this.lnum = lnum |
| 83 | this.col = col |
| 84 | enddef |
| 85 | |
| 86 | def SetLnum(lnum: number) |
| 87 | this.lnum = lnum |
| 88 | enddef |
| 89 | |
| 90 | def SetCol(col: number) |
| 91 | this.col = col |
| 92 | enddef |
| 93 | |
| 94 | def SetPosition(lnum: number, col: number) |
| 95 | this.lnum = lnum |
| 96 | this.col = col |
| 97 | enddef |
| 98 | endclass |
Bram Moolenaar | 7db29e4 | 2022-12-11 15:53:04 +0000 | [diff] [blame] | 99 | < *object* *Object* |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 100 | You can create an object from this class with the new() method: > |
| 101 | |
| 102 | var pos = TextPosition.new(1, 1) |
errael | 1d4fcfe | 2023-12-21 08:34:15 -0800 | [diff] [blame] | 103 | < |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 104 | The object variables "lnum" and "col" can be accessed directly: > |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 105 | |
| 106 | echo $'The text position is ({pos.lnum}, {pos.col})' |
Christian Brabandt | 6c1afa3 | 2024-01-01 20:50:51 +0100 | [diff] [blame] | 107 | < *E1317* *E1327* *:this* |
errael | 1d4fcfe | 2023-12-21 08:34:15 -0800 | [diff] [blame] | 108 | If you have been using other object-oriented languages you will notice that in |
Aliaksei Budavei | b214995 | 2024-01-07 17:52:10 +0300 | [diff] [blame] | 109 | Vim, within a class definition, the declared object members are consistently |
| 110 | referred to with the "this." prefix. This is different from languages like |
| 111 | Java and TypeScript. The naming convention makes the object members easy to |
| 112 | spot. Also, when a variable does not have the "this." prefix you know it is |
| 113 | not an object variable. |
errael | 1d4fcfe | 2023-12-21 08:34:15 -0800 | [diff] [blame] | 114 | *E1411* |
| 115 | From outside the class definition, access an object's methods and variables by |
| 116 | using the object name followed by a dot following by the member: > |
| 117 | |
| 118 | pos.lnum |
Aliaksei Budavei | b214995 | 2024-01-07 17:52:10 +0300 | [diff] [blame] | 119 | pos.SetCol(10) |
errael | 1d4fcfe | 2023-12-21 08:34:15 -0800 | [diff] [blame] | 120 | < |
| 121 | *E1405* *E1406* |
| 122 | A class name cannot be used as an expression. A class name cannot be used in |
| 123 | the left-hand-side of an assignment. |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 124 | |
| 125 | |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 126 | Object variable write access ~ |
Ernie Rael | 03042a2 | 2023-11-11 08:53:32 +0100 | [diff] [blame] | 127 | *read-only-variable* |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 128 | Now try to change an object variable directly: > |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 129 | |
| 130 | pos.lnum = 9 |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 131 | < *E1335* |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 132 | This will give you an error! That is because by default object variables can |
| 133 | be read but not set. That's why the TextPosition class provides a method for |
| 134 | it: > |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 135 | |
| 136 | pos.SetLnum(9) |
| 137 | |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 138 | Allowing to read but not set an object variable is the most common and safest |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 139 | way. Most often there is no problem using a value, while setting a value may |
| 140 | have side effects that need to be taken care of. In this case, the SetLnum() |
| 141 | method could check if the line number is valid and either give an error or use |
| 142 | the closest valid value. |
Ernie Rael | 03042a2 | 2023-11-11 08:53:32 +0100 | [diff] [blame] | 143 | *:public* *public-variable* *E1331* |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 144 | If you don't care about side effects and want to allow the object variable to |
| 145 | be changed at any time, you can make it public: > |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 146 | |
Aliaksei Budavei | 0aed99a | 2023-12-29 01:08:24 +0300 | [diff] [blame] | 147 | public var lnum: number |
| 148 | public var col: number |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 149 | |
| 150 | Now you don't need the SetLnum(), SetCol() and SetPosition() methods, setting |
| 151 | "pos.lnum" directly above will no longer give an error. |
Yegappan Lakshmanan | 00cd182 | 2023-09-18 19:56:49 +0200 | [diff] [blame] | 152 | *E1326* |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 153 | If you try to set an object variable that doesn't exist you get an error: > |
Bram Moolenaar | f1dcd14 | 2022-12-31 15:30:45 +0000 | [diff] [blame] | 154 | pos.other = 9 |
Yegappan Lakshmanan | 00cd182 | 2023-09-18 19:56:49 +0200 | [diff] [blame] | 155 | < E1326: Member not found on object "TextPosition": other ~ |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 156 | |
Yegappan Lakshmanan | 00cd182 | 2023-09-18 19:56:49 +0200 | [diff] [blame] | 157 | *E1376* |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 158 | A object variable cannot be accessed using the class name. |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 159 | |
Ernie Rael | 03042a2 | 2023-11-11 08:53:32 +0100 | [diff] [blame] | 160 | Protected variables ~ |
| 161 | *protected-variable* *E1332* *E1333* |
| 162 | On the other hand, if you do not want the object variables to be read directly |
| 163 | from outside the class or its sub-classes, you can make them protected. This |
| 164 | is done by prefixing an underscore to the name: > |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 165 | |
Doug Kearns | 74da0ee | 2023-12-14 20:26:26 +0100 | [diff] [blame] | 166 | var _lnum: number |
Aliaksei Budavei | b214995 | 2024-01-07 17:52:10 +0300 | [diff] [blame] | 167 | var _col: number |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 168 | |
Ernie Rael | 03042a2 | 2023-11-11 08:53:32 +0100 | [diff] [blame] | 169 | Now you need to provide methods to get the value of the protected variables. |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 170 | These are commonly called getters. We recommend using a name that starts with |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 171 | "Get": > |
| 172 | |
| 173 | def GetLnum(): number |
| 174 | return this._lnum |
| 175 | enddef |
| 176 | |
Aliaksei Budavei | b214995 | 2024-01-07 17:52:10 +0300 | [diff] [blame] | 177 | def GetCol(): number |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 178 | return this._col |
| 179 | enddef |
| 180 | |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 181 | This example isn't very useful, the variables might as well have been public. |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 182 | It does become useful if you check the value. For example, restrict the line |
| 183 | number to the total number of lines: > |
| 184 | |
| 185 | def GetLnum(): number |
| 186 | if this._lnum > this._lineCount |
| 187 | return this._lineCount |
| 188 | endif |
| 189 | return this._lnum |
| 190 | enddef |
Yegappan Lakshmanan | 00cd182 | 2023-09-18 19:56:49 +0200 | [diff] [blame] | 191 | < |
Ernie Rael | 03042a2 | 2023-11-11 08:53:32 +0100 | [diff] [blame] | 192 | Protected methods ~ |
| 193 | *protected-method* *E1366* |
Yegappan Lakshmanan | cd7293b | 2023-08-27 19:18:23 +0200 | [diff] [blame] | 194 | If you want object methods to be accessible only from other methods of the |
| 195 | same class and not used from outside the class, then you can make them |
Ernie Rael | 03042a2 | 2023-11-11 08:53:32 +0100 | [diff] [blame] | 196 | protected. This is done by prefixing the method name with an underscore: > |
Yegappan Lakshmanan | cd7293b | 2023-08-27 19:18:23 +0200 | [diff] [blame] | 197 | |
| 198 | class SomeClass |
| 199 | def _Foo(): number |
| 200 | return 10 |
| 201 | enddef |
| 202 | def Bar(): number |
| 203 | return this._Foo() |
| 204 | enddef |
| 205 | endclass |
| 206 | < |
Ernie Rael | 03042a2 | 2023-11-11 08:53:32 +0100 | [diff] [blame] | 207 | Accessing a protected method outside the class will result in an error (using |
Yegappan Lakshmanan | cd7293b | 2023-08-27 19:18:23 +0200 | [diff] [blame] | 208 | the above class): > |
| 209 | |
| 210 | var a = SomeClass.new() |
| 211 | a._Foo() |
| 212 | < |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 213 | Simplifying the new() method ~ |
Yegappan Lakshmanan | 413f839 | 2023-09-28 22:46:37 +0200 | [diff] [blame] | 214 | *new()* *constructor* |
errael | 92feeaf | 2023-12-27 10:07:09 -0800 | [diff] [blame] | 215 | See also |default-constructor| and |multiple-constructors|. |
| 216 | |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 217 | Many constructors take values for the object variables. Thus you very often |
| 218 | see this pattern: > |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 219 | |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 220 | class SomeClass |
Doug Kearns | 74da0ee | 2023-12-14 20:26:26 +0100 | [diff] [blame] | 221 | var lnum: number |
| 222 | var col: number |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 223 | |
| 224 | def new(lnum: number, col: number) |
| 225 | this.lnum = lnum |
| 226 | this.col = col |
| 227 | enddef |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 228 | endclass |
h-east | db38552 | 2023-09-28 22:18:19 +0200 | [diff] [blame] | 229 | < |
| 230 | *E1390* |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 231 | Not only is this text you need to write, it also has the type of each |
Dominique Pellé | 17dca3c | 2023-12-14 20:36:32 +0100 | [diff] [blame] | 232 | variable twice. Since this is so common a shorter way to write new() is |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 233 | provided: > |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 234 | |
| 235 | def new(this.lnum, this.col) |
| 236 | enddef |
| 237 | |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 238 | The semantics are easy to understand: Providing the object variable name, |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 239 | including "this.", as the argument to new() means the value provided in the |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 240 | new() call is assigned to that object variable. This mechanism comes from the |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 241 | Dart language. |
| 242 | |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 243 | Putting together this way of using new() and making the variables public |
| 244 | results in a much shorter class definition than what we started with: > |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 245 | |
| 246 | class TextPosition |
Doug Kearns | 74da0ee | 2023-12-14 20:26:26 +0100 | [diff] [blame] | 247 | public var lnum: number |
| 248 | public var col: number |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 249 | |
| 250 | def new(this.lnum, this.col) |
| 251 | enddef |
| 252 | |
| 253 | def SetPosition(lnum: number, col: number) |
| 254 | this.lnum = lnum |
| 255 | this.col = col |
| 256 | enddef |
| 257 | endclass |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 258 | |
| 259 | The sequence of constructing a new object is: |
| 260 | 1. Memory is allocated and cleared. All values are zero/false/empty. |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 261 | 2. For each declared object variable that has an initializer, the expression |
| 262 | is evaluated and assigned to the variable. This happens in the sequence |
| 263 | the variables are declared in the class. |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 264 | 3. Arguments in the new() method in the "this.name" form are assigned. |
| 265 | 4. The body of the new() method is executed. |
| 266 | |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 267 | If the class extends a parent class, the same thing happens. In the second |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 268 | step the object variables of the parent class are initialized first. There is |
| 269 | no need to call "super()" or "new()" on the parent. |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 270 | |
Yegappan Lakshmanan | b90e3bc | 2023-09-28 23:06:48 +0200 | [diff] [blame] | 271 | *E1365* |
Yegappan Lakshmanan | 6ac1544 | 2023-08-20 18:20:17 +0200 | [diff] [blame] | 272 | When defining the new() method the return type should not be specified. It |
| 273 | always returns an object of the class. |
| 274 | |
Yegappan Lakshmanan | b90e3bc | 2023-09-28 23:06:48 +0200 | [diff] [blame] | 275 | *E1386* |
| 276 | When invoking an object method, the method name should be preceded by the |
Dominique Pellé | 17dca3c | 2023-12-14 20:36:32 +0100 | [diff] [blame] | 277 | object variable name. An object method cannot be invoked using the class |
Yegappan Lakshmanan | b90e3bc | 2023-09-28 23:06:48 +0200 | [diff] [blame] | 278 | name. |
| 279 | |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 280 | ============================================================================== |
| 281 | |
Yegappan Lakshmanan | 00cd182 | 2023-09-18 19:56:49 +0200 | [diff] [blame] | 282 | 3. Class Variables and Methods *Vim9-class-member* |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 283 | |
Yegappan Lakshmanan | 00cd182 | 2023-09-18 19:56:49 +0200 | [diff] [blame] | 284 | *:static* *E1337* *E1338* *E1368* |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 285 | Class members are declared with "static". They are used by the name without a |
Yegappan Lakshmanan | 00cd182 | 2023-09-18 19:56:49 +0200 | [diff] [blame] | 286 | prefix in the class where they are defined: > |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 287 | |
| 288 | class OtherThing |
Doug Kearns | 74da0ee | 2023-12-14 20:26:26 +0100 | [diff] [blame] | 289 | var size: number |
| 290 | static var totalSize: number |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 291 | |
| 292 | def new(this.size) |
| 293 | totalSize += this.size |
| 294 | enddef |
| 295 | endclass |
| 296 | < *E1340* *E1341* |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 297 | Since the name is used as-is, shadowing the name by a method argument name |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 298 | or local variable name is not allowed. |
| 299 | |
Yegappan Lakshmanan | b90e3bc | 2023-09-28 23:06:48 +0200 | [diff] [blame] | 300 | *E1374* *E1375* *E1384* *E1385* |
Yegappan Lakshmanan | 00cd182 | 2023-09-18 19:56:49 +0200 | [diff] [blame] | 301 | To access a class member outside of the class where it is defined, the class |
| 302 | name prefix must be used. A class member cannot be accessed using an object. |
| 303 | |
Ernie Rael | 03042a2 | 2023-11-11 08:53:32 +0100 | [diff] [blame] | 304 | Just like object members the access can be made protected by using an |
| 305 | underscore as the first character in the name, and it can be made public by |
| 306 | prefixing "public": > |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 307 | |
| 308 | class OtherThing |
Doug Kearns | 74da0ee | 2023-12-14 20:26:26 +0100 | [diff] [blame] | 309 | static var total: number # anybody can read, only class can write |
| 310 | static var _sum: number # only class can read and write |
| 311 | public static var result: number # anybody can read and write |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 312 | endclass |
| 313 | < |
Yegappan Lakshmanan | 00cd182 | 2023-09-18 19:56:49 +0200 | [diff] [blame] | 314 | *class-method* |
| 315 | Class methods are also declared with "static". They can use the class |
| 316 | variables but they have no access to the object variables, they cannot use the |
h_east | ba77bbb | 2023-10-03 04:47:13 +0900 | [diff] [blame] | 317 | "this" keyword: |
Yegappan Lakshmanan | 00cd182 | 2023-09-18 19:56:49 +0200 | [diff] [blame] | 318 | > |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 319 | class OtherThing |
Doug Kearns | 74da0ee | 2023-12-14 20:26:26 +0100 | [diff] [blame] | 320 | var size: number |
| 321 | static var totalSize: number |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 322 | |
| 323 | # Clear the total size and return the value it had before. |
| 324 | static def ClearTotalSize(): number |
| 325 | var prev = totalSize |
| 326 | totalSize = 0 |
| 327 | return prev |
| 328 | enddef |
| 329 | endclass |
| 330 | |
Yegappan Lakshmanan | 00cd182 | 2023-09-18 19:56:49 +0200 | [diff] [blame] | 331 | Inside the class the class method can be called by name directly, outside the |
| 332 | class the class name must be prefixed: `OtherThing.ClearTotalSize()`. To use |
Aliaksei Budavei | b214995 | 2024-01-07 17:52:10 +0300 | [diff] [blame] | 333 | a class method from a parent class in a child class, the class name must be |
| 334 | prefixed. |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 335 | |
Ernie Rael | 03042a2 | 2023-11-11 08:53:32 +0100 | [diff] [blame] | 336 | Just like object methods the access can be made protected by using an |
| 337 | underscore as the first character in the method name: > |
Yegappan Lakshmanan | cd7293b | 2023-08-27 19:18:23 +0200 | [diff] [blame] | 338 | |
| 339 | class OtherThing |
| 340 | static def _Foo() |
| 341 | echo "Foo" |
| 342 | enddef |
| 343 | def Bar() |
Yegappan Lakshmanan | 00cd182 | 2023-09-18 19:56:49 +0200 | [diff] [blame] | 344 | _Foo() |
Yegappan Lakshmanan | cd7293b | 2023-08-27 19:18:23 +0200 | [diff] [blame] | 345 | enddef |
| 346 | endclass |
Gianmaria Bajo | 4b9777a | 2023-08-29 22:26:30 +0200 | [diff] [blame] | 347 | < |
| 348 | *E1370* |
Ernie Rael | 4e28631 | 2023-12-24 11:03:31 +0100 | [diff] [blame] | 349 | Note that constructors cannot be declared as "static". They are called like a |
| 350 | static but execute as an object method; they have access to "this". |
Yegappan Lakshmanan | cd7293b | 2023-08-27 19:18:23 +0200 | [diff] [blame] | 351 | |
Yegappan Lakshmanan | 00cd182 | 2023-09-18 19:56:49 +0200 | [diff] [blame] | 352 | To access the class methods and class variables of a super class in an |
| 353 | extended class, the class name prefix should be used just as from anywhere |
| 354 | outside of the defining class: > |
| 355 | |
| 356 | vim9script |
| 357 | class Vehicle |
Doug Kearns | 74da0ee | 2023-12-14 20:26:26 +0100 | [diff] [blame] | 358 | static var nextID: number = 1000 |
Yegappan Lakshmanan | 00cd182 | 2023-09-18 19:56:49 +0200 | [diff] [blame] | 359 | static def GetID(): number |
| 360 | nextID += 1 |
| 361 | return nextID |
| 362 | enddef |
| 363 | endclass |
| 364 | class Car extends Vehicle |
Doug Kearns | 74da0ee | 2023-12-14 20:26:26 +0100 | [diff] [blame] | 365 | var myID: number |
Yegappan Lakshmanan | 00cd182 | 2023-09-18 19:56:49 +0200 | [diff] [blame] | 366 | def new() |
| 367 | this.myID = Vehicle.GetID() |
| 368 | enddef |
| 369 | endclass |
| 370 | < |
| 371 | Class variables and methods are not inherited by a child class. A child class |
| 372 | can declare a static variable or a method with the same name as the one in the |
| 373 | super class. Depending on the class where the member is used the |
| 374 | corresponding class member will be used. The type of the class member in a |
| 375 | child class can be different from that in the super class. |
| 376 | |
Yegappan Lakshmanan | ff6f0d5 | 2023-12-21 16:46:18 +0100 | [diff] [blame] | 377 | The double underscore (__) prefix for a class or object method name is |
| 378 | reserved for future use. |
| 379 | |
Yegappan Lakshmanan | e5437c5 | 2023-12-16 14:11:19 +0100 | [diff] [blame] | 380 | *object-final-variable* *E1409* |
| 381 | The |:final| keyword can be used to make a class or object variable a |
| 382 | constant. Examples: > |
| 383 | |
| 384 | class A |
| 385 | final v1 = [1, 2] # final object variable |
| 386 | public final v2 = {x: 1} # final object variable |
| 387 | static final v3 = 'abc' # final class variable |
| 388 | public static final v4 = 0z10 # final class variable |
| 389 | endclass |
| 390 | < |
| 391 | A final variable can be changed only from a constructor function. Example: > |
| 392 | |
| 393 | class A |
| 394 | final v1: list<number> |
| 395 | def new() |
| 396 | this.v1 = [1, 2] |
| 397 | enddef |
| 398 | endclass |
| 399 | var a = A.new() |
| 400 | echo a.v1 |
| 401 | < |
| 402 | Note that the value of a final variable can be changed. Example: > |
| 403 | |
| 404 | class A |
| 405 | public final v1 = [1, 2] |
| 406 | endclass |
| 407 | var a = A.new() |
| 408 | a.v1[0] = 6 # OK |
| 409 | a.v1->add(3) # OK |
| 410 | a.v1 = [3, 4] # Error |
| 411 | < |
| 412 | *E1408* |
| 413 | Final variables are not supported in an interface. A class or object method |
| 414 | cannot be final. |
| 415 | |
| 416 | *object-const-variable* |
| 417 | The |:const| keyword can be used to make a class or object variable and the |
| 418 | value a constant. Examples: > |
| 419 | |
| 420 | class A |
| 421 | const v1 = [1, 2] # const object variable |
| 422 | public const v2 = {x: 1} # const object variable |
| 423 | static const v3 = 'abc' # const class variable |
| 424 | public static const v4 = 0z10 # const class variable |
| 425 | endclass |
| 426 | < |
| 427 | A const variable can be changed only from a constructor function. Example: > |
| 428 | |
| 429 | class A |
| 430 | const v1: list<number> |
| 431 | def new() |
| 432 | this.v1 = [1, 2] |
| 433 | enddef |
| 434 | endclass |
| 435 | var a = A.new() |
| 436 | echo a.v1 |
| 437 | < |
| 438 | A const variable and its value cannot be changed. Example: > |
| 439 | |
| 440 | class A |
| 441 | public const v1 = [1, 2] |
| 442 | endclass |
| 443 | var a = A.new() |
| 444 | a.v1[0] = 6 # Error |
| 445 | a.v1->add(3) # Error |
| 446 | a.v1 = [3, 4] # Error |
| 447 | < |
| 448 | *E1410* |
| 449 | Const variables are not supported in an interface. A class or object method |
| 450 | cannot be a const. |
| 451 | |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 452 | ============================================================================== |
| 453 | |
| 454 | 4. Using an abstract class *Vim9-abstract-class* |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 455 | |
| 456 | An abstract class forms the base for at least one sub-class. In the class |
| 457 | model one often finds that a few classes have the same properties that can be |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 458 | shared, but a class with these properties does not have enough state to create |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 459 | an object from. A sub-class must extend the abstract class and add the |
| 460 | missing state and/or methods before it can be used to create objects for. |
| 461 | |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 462 | For example, a Shape class could store a color and thickness. You cannot |
| 463 | create a Shape object, it is missing the information about what kind of shape |
| 464 | it is. The Shape class functions as the base for a Square and a Triangle |
| 465 | class, for which objects can be created. Example: > |
| 466 | |
| 467 | abstract class Shape |
Doug Kearns | 74da0ee | 2023-12-14 20:26:26 +0100 | [diff] [blame] | 468 | var color = Color.Black |
| 469 | var thickness = 10 |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 470 | endclass |
| 471 | |
| 472 | class Square extends Shape |
Doug Kearns | 74da0ee | 2023-12-14 20:26:26 +0100 | [diff] [blame] | 473 | var size: number |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 474 | |
| 475 | def new(this.size) |
| 476 | enddef |
| 477 | endclass |
| 478 | |
| 479 | class Triangle extends Shape |
Doug Kearns | 74da0ee | 2023-12-14 20:26:26 +0100 | [diff] [blame] | 480 | var base: number |
| 481 | var height: number |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 482 | |
| 483 | def new(this.base, this.height) |
| 484 | enddef |
| 485 | endclass |
| 486 | < |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 487 | An abstract class is defined the same way as a normal class, except that it |
| 488 | does not have any new() method. *E1359* |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 489 | |
h_east | 596a9f2 | 2023-11-21 21:24:23 +0900 | [diff] [blame] | 490 | *abstract-method* *E1371* *E1372* |
Yegappan Lakshmanan | 7bcd25c | 2023-09-08 19:27:51 +0200 | [diff] [blame] | 491 | An abstract method can be defined in an abstract class by using the "abstract" |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 492 | prefix when defining the method: > |
Yegappan Lakshmanan | 7bcd25c | 2023-09-08 19:27:51 +0200 | [diff] [blame] | 493 | |
| 494 | abstract class Shape |
| 495 | abstract def Draw() |
| 496 | endclass |
Yegappan Lakshmanan | 00cd182 | 2023-09-18 19:56:49 +0200 | [diff] [blame] | 497 | < |
Yegappan Lakshmanan | ef9e3f8 | 2023-11-02 20:43:57 +0100 | [diff] [blame] | 498 | A static method in an abstract class cannot be an abstract method. |
| 499 | |
Yegappan Lakshmanan | 00cd182 | 2023-09-18 19:56:49 +0200 | [diff] [blame] | 500 | *E1373* |
Aliaksei Budavei | b214995 | 2024-01-07 17:52:10 +0300 | [diff] [blame] | 501 | A non-abstract class extending the abstract class must implement all the |
| 502 | abstract methods. The signature (arguments, argument types and return type) |
| 503 | must be exactly the same. If the return type of a method is a class, then |
| 504 | that class or one of its subclasses can be used in the extended method. |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 505 | |
| 506 | ============================================================================== |
| 507 | |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 508 | 5. Using an interface *Vim9-using-interface* |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 509 | |
| 510 | The example above with Shape, Square and Triangle can be made more useful if |
| 511 | we add a method to compute the surface of the object. For that we create the |
| 512 | interface called HasSurface, which specifies one method Surface() that returns |
| 513 | a number. This example extends the one above: > |
| 514 | |
| 515 | abstract class Shape |
Doug Kearns | 74da0ee | 2023-12-14 20:26:26 +0100 | [diff] [blame] | 516 | var color = Color.Black |
| 517 | var thickness = 10 |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 518 | endclass |
| 519 | |
| 520 | interface HasSurface |
| 521 | def Surface(): number |
| 522 | endinterface |
| 523 | |
| 524 | class Square extends Shape implements HasSurface |
Doug Kearns | 74da0ee | 2023-12-14 20:26:26 +0100 | [diff] [blame] | 525 | var size: number |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 526 | |
| 527 | def new(this.size) |
| 528 | enddef |
| 529 | |
| 530 | def Surface(): number |
| 531 | return this.size * this.size |
| 532 | enddef |
| 533 | endclass |
| 534 | |
| 535 | class Triangle extends Shape implements HasSurface |
Doug Kearns | 74da0ee | 2023-12-14 20:26:26 +0100 | [diff] [blame] | 536 | var base: number |
| 537 | var height: number |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 538 | |
| 539 | def new(this.base, this.height) |
| 540 | enddef |
| 541 | |
| 542 | def Surface(): number |
| 543 | return this.base * this.height / 2 |
| 544 | enddef |
| 545 | endclass |
Yegappan Lakshmanan | 00cd182 | 2023-09-18 19:56:49 +0200 | [diff] [blame] | 546 | < |
| 547 | *E1348* *E1349* *E1367* *E1382* *E1383* |
Bram Moolenaar | 1b5f03e | 2023-01-09 20:12:45 +0000 | [diff] [blame] | 548 | If a class declares to implement an interface, all the items specified in the |
Yegappan Lakshmanan | 00cd182 | 2023-09-18 19:56:49 +0200 | [diff] [blame] | 549 | interface must appear in the class, with the same types. |
Bram Moolenaar | 1b5f03e | 2023-01-09 20:12:45 +0000 | [diff] [blame] | 550 | |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 551 | The interface name can be used as a type: > |
| 552 | |
| 553 | var shapes: list<HasSurface> = [ |
| 554 | Square.new(12), |
| 555 | Triangle.new(8, 15), |
| 556 | ] |
| 557 | for shape in shapes |
| 558 | echo $'the surface is {shape.Surface()}' |
| 559 | endfor |
Yegappan Lakshmanan | 00cd182 | 2023-09-18 19:56:49 +0200 | [diff] [blame] | 560 | < |
Yegappan Lakshmanan | 2dede3d | 2023-09-27 19:02:01 +0200 | [diff] [blame] | 561 | *E1378* *E1379* *E1380* *E1387* |
| 562 | An interface can contain only object methods and read-only object variables. |
Ernie Rael | 03042a2 | 2023-11-11 08:53:32 +0100 | [diff] [blame] | 563 | An interface cannot contain read-write or protected object variables, |
| 564 | protected object methods, class variables and class methods. |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 565 | |
Yegappan Lakshmanan | 00cd182 | 2023-09-18 19:56:49 +0200 | [diff] [blame] | 566 | An interface can extend another interface using "extends". The sub-interface |
| 567 | inherits all the instance variables and methods from the super interface. |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 568 | |
| 569 | ============================================================================== |
| 570 | |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 571 | 6. More class details *Vim9-class* *Class* *class* |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 572 | |
| 573 | Defining a class ~ |
| 574 | *:class* *:endclass* *:abstract* |
| 575 | A class is defined between `:class` and `:endclass`. The whole class is |
| 576 | defined in one script file. It is not possible to add to a class later. |
| 577 | |
Bram Moolenaar | 7db29e4 | 2022-12-11 15:53:04 +0000 | [diff] [blame] | 578 | A class can only be defined in a |Vim9| script file. *E1316* |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 579 | A class cannot be defined inside a function. |
| 580 | |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 581 | It is possible to define more than one class in a script file. Although it |
| 582 | usually is better to export only one main class. It can be useful to define |
| 583 | types, enums and helper classes though. |
| 584 | |
| 585 | The `:abstract` keyword may be prefixed and `:export` may be used. That gives |
| 586 | these variants: > |
| 587 | |
| 588 | class ClassName |
| 589 | endclass |
| 590 | |
| 591 | export class ClassName |
| 592 | endclass |
| 593 | |
| 594 | abstract class ClassName |
| 595 | endclass |
| 596 | |
| 597 | export abstract class ClassName |
| 598 | endclass |
| 599 | < |
| 600 | *E1314* |
| 601 | The class name should be CamelCased. It must start with an uppercase letter. |
| 602 | That avoids clashing with builtin types. |
Bram Moolenaar | 7db29e4 | 2022-12-11 15:53:04 +0000 | [diff] [blame] | 603 | *E1315* |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 604 | After the class name these optional items can be used. Each can appear only |
| 605 | once. They can appear in any order, although this order is recommended: > |
| 606 | extends ClassName |
| 607 | implements InterfaceName, OtherInterface |
| 608 | specifies SomeInterface |
Yegappan Lakshmanan | 00cd182 | 2023-09-18 19:56:49 +0200 | [diff] [blame] | 609 | < *E1355* *E1369* |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 610 | Each variable and method name can be used only once. It is not possible to |
| 611 | define a method with the same name and different type of arguments. It is not |
Aliaksei Budavei | b214995 | 2024-01-07 17:52:10 +0300 | [diff] [blame] | 612 | possible to use a public and protected member variable with the same name. An |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 613 | object variable name used in a super class cannot be reused in a child class. |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 614 | |
| 615 | |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 616 | Object Variable Initialization ~ |
Ernie Rael | 03042a2 | 2023-11-11 08:53:32 +0100 | [diff] [blame] | 617 | |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 618 | If the type of a variable is not explicitly specified in a class, then it is |
| 619 | set to "any" during class definition. When an object is instantiated from the |
| 620 | class, then the type of the variable is set. |
Yegappan Lakshmanan | 618e47d | 2023-08-22 21:29:28 +0200 | [diff] [blame] | 621 | |
Yegappan Lakshmanan | f3b68d4 | 2023-09-29 22:50:02 +0200 | [diff] [blame] | 622 | The following reserved keyword names cannot be used as an object or class |
| 623 | variable name: "super", "this", "true", "false", "null", "null_blob", |
| 624 | "null_dict", "null_function", "null_list", "null_partial", "null_string", |
| 625 | "null_channel" and "null_job". |
Yegappan Lakshmanan | 618e47d | 2023-08-22 21:29:28 +0200 | [diff] [blame] | 626 | |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 627 | Extending a class ~ |
| 628 | *extends* |
Bram Moolenaar | 1b5f03e | 2023-01-09 20:12:45 +0000 | [diff] [blame] | 629 | A class can extend one other class. *E1352* *E1353* *E1354* |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 630 | The basic idea is to build on top of an existing class, add properties to it. |
| 631 | |
| 632 | The extended class is called the "base class" or "super class". The new class |
| 633 | is called the "child class". |
| 634 | |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 635 | Object variables from the base class are all taken over by the child class. It |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 636 | is not possible to override them (unlike some other languages). |
| 637 | |
| 638 | *E1356* *E1357* *E1358* |
Yegappan Lakshmanan | b32064f | 2023-10-02 21:43:58 +0200 | [diff] [blame] | 639 | Object methods of the base class can be overruled. The signature (arguments, |
Yegappan Lakshmanan | 26e8f7b | 2023-10-06 10:24:10 -0700 | [diff] [blame] | 640 | argument types and return type) must be exactly the same. If the return type |
| 641 | of a method is a class, then that class or one of its subclasses can be used |
| 642 | in the extended method. The method of the base class can be called by |
| 643 | prefixing "super.". |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 644 | |
Yegappan Lakshmanan | 00cd182 | 2023-09-18 19:56:49 +0200 | [diff] [blame] | 645 | *E1377* |
Ernie Rael | 03042a2 | 2023-11-11 08:53:32 +0100 | [diff] [blame] | 646 | The access level of a method (public or protected) in a child class should be |
Yegappan Lakshmanan | 00cd182 | 2023-09-18 19:56:49 +0200 | [diff] [blame] | 647 | the same as the super class. |
| 648 | |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 649 | Other object methods of the base class are taken over by the child class. |
| 650 | |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 651 | Class methods, including methods starting with "new", can be overruled, like |
| 652 | with object methods. The method on the base class can be called by prefixing |
| 653 | the name of the class (for class methods) or "super.". |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 654 | |
| 655 | Unlike other languages, the constructor of the base class does not need to be |
| 656 | invoked. In fact, it cannot be invoked. If some initialization from the base |
| 657 | class also needs to be done in a child class, put it in an object method and |
| 658 | call that method from every constructor(). |
| 659 | |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 660 | If the base class did not specify a new() method then one was automatically |
| 661 | created. This method will not be taken over by the child class. The child |
| 662 | class can define its own new() method, or, if there isn't one, a new() method |
| 663 | will be added automatically. |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 664 | |
| 665 | |
| 666 | A class implementing an interface ~ |
Yegappan Lakshmanan | 2dede3d | 2023-09-27 19:02:01 +0200 | [diff] [blame] | 667 | *implements* *E1346* *E1347* *E1389* |
Bram Moolenaar | 1b5f03e | 2023-01-09 20:12:45 +0000 | [diff] [blame] | 668 | A class can implement one or more interfaces. The "implements" keyword can |
| 669 | only appear once *E1350* . Multiple interfaces can be specified, separated by |
| 670 | commas. Each interface name can appear only once. *E1351* |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 671 | |
| 672 | |
| 673 | A class defining an interface ~ |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 674 | *specifies* |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 675 | A class can declare its interface, the object variables and methods, with a |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 676 | named interface. This avoids the need for separately specifying the |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 677 | interface, which is often done in many languages, especially Java. |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 678 | |
| 679 | |
Bram Moolenaar | 7db29e4 | 2022-12-11 15:53:04 +0000 | [diff] [blame] | 680 | Items in a class ~ |
Yegappan Lakshmanan | 2dede3d | 2023-09-27 19:02:01 +0200 | [diff] [blame] | 681 | *E1318* *E1325* *E1388* |
Bram Moolenaar | dd60c36 | 2023-02-27 15:49:53 +0000 | [diff] [blame] | 682 | Inside a class, in between `:class` and `:endclass`, these items can appear: |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 683 | - An object variable declaration: > |
Doug Kearns | 74da0ee | 2023-12-14 20:26:26 +0100 | [diff] [blame] | 684 | var _protectedVariableName: memberType |
| 685 | var readonlyVariableName: memberType |
| 686 | public var readwriteVariableName: memberType |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 687 | - A class variable declaration: > |
Doug Kearns | 74da0ee | 2023-12-14 20:26:26 +0100 | [diff] [blame] | 688 | static var _protectedClassVariableName: memberType |
| 689 | static var readonlyClassVariableName: memberType |
Aliaksei Budavei | b214995 | 2024-01-07 17:52:10 +0300 | [diff] [blame] | 690 | public static var readwriteClassVariableName: memberType |
Bram Moolenaar | 7db29e4 | 2022-12-11 15:53:04 +0000 | [diff] [blame] | 691 | - A constructor method: > |
Bram Moolenaar | 938ae28 | 2023-02-20 20:44:55 +0000 | [diff] [blame] | 692 | def new(arguments) |
| 693 | def newName(arguments) |
Yegappan Lakshmanan | 00cd182 | 2023-09-18 19:56:49 +0200 | [diff] [blame] | 694 | - A class method: > |
| 695 | static def SomeMethod(arguments) |
Ernie Rael | 03042a2 | 2023-11-11 08:53:32 +0100 | [diff] [blame] | 696 | static def _ProtectedMethod(arguments) |
Bram Moolenaar | 7db29e4 | 2022-12-11 15:53:04 +0000 | [diff] [blame] | 697 | - An object method: > |
Bram Moolenaar | 938ae28 | 2023-02-20 20:44:55 +0000 | [diff] [blame] | 698 | def SomeMethod(arguments) |
Ernie Rael | 03042a2 | 2023-11-11 08:53:32 +0100 | [diff] [blame] | 699 | def _ProtectedMethod(arguments) |
Yegappan Lakshmanan | 00cd182 | 2023-09-18 19:56:49 +0200 | [diff] [blame] | 700 | |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 701 | For the object variable the type must be specified. The best way is to do |
| 702 | this explicitly with ": {type}". For simple types you can also use an |
| 703 | initializer, such as "= 123", and Vim will see that the type is a number. |
| 704 | Avoid doing this for more complex types and when the type will be incomplete. |
| 705 | For example: > |
Doug Kearns | 74da0ee | 2023-12-14 20:26:26 +0100 | [diff] [blame] | 706 | var nameList = [] |
Bram Moolenaar | f1dcd14 | 2022-12-31 15:30:45 +0000 | [diff] [blame] | 707 | This specifies a list, but the item type is unknown. Better use: > |
Doug Kearns | 74da0ee | 2023-12-14 20:26:26 +0100 | [diff] [blame] | 708 | var nameList: list<string> |
Bram Moolenaar | f1dcd14 | 2022-12-31 15:30:45 +0000 | [diff] [blame] | 709 | The initialization isn't needed, the list is empty by default. |
| 710 | *E1330* |
| 711 | Some types cannot be used, such as "void", "null" and "v:none". |
Bram Moolenaar | 7db29e4 | 2022-12-11 15:53:04 +0000 | [diff] [blame] | 712 | |
Yegappan Lakshmanan | d3eae7b | 2024-03-03 16:26:58 +0100 | [diff] [blame] | 713 | Builtin Object Methods ~ |
| 714 | *builtin-object-methods* |
| 715 | Some of the builtin functions like |empty()|, |len()| and |string()| can be |
| 716 | used with an object. An object can implement a method with the same name as |
| 717 | these builtin functions to return an object-specific value. |
| 718 | |
| 719 | *E1412* |
| 720 | The following builtin methods are supported: |
| 721 | *object-empty()* |
| 722 | empty() Invoked by the |empty()| function to check whether an object is |
| 723 | empty. If this method is missing, then true is returned. This |
| 724 | method should not accept any arguments and must return a boolean. |
| 725 | *object-len()* |
| 726 | len() Invoked by the |len()| function to return the length of an |
| 727 | object. If this method is missing in the class, then an error is |
| 728 | given and zero is returned. This method should not accept any |
| 729 | arguments and must return a number. |
| 730 | *object-string()* |
| 731 | string() Invoked by the |string()| function to get a textual |
| 732 | representation of an object. Also used by the |:echo| command |
| 733 | for an object. If this method is missing in the class, then a |
| 734 | built-in default textual representation is used. This method |
| 735 | should not accept any arguments and must return a string. |
| 736 | |
| 737 | *E1413* |
| 738 | A class method cannot be used as a builtin method. |
Bram Moolenaar | 7db29e4 | 2022-12-11 15:53:04 +0000 | [diff] [blame] | 739 | |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 740 | Defining an interface ~ |
Christian Brabandt | 6c1afa3 | 2024-01-01 20:50:51 +0100 | [diff] [blame] | 741 | *Interface* *:interface* *:endinterface* |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 742 | An interface is defined between `:interface` and `:endinterface`. It may be |
| 743 | prefixed with `:export`: > |
| 744 | |
| 745 | interface InterfaceName |
| 746 | endinterface |
| 747 | |
| 748 | export interface InterfaceName |
| 749 | endinterface |
Bram Moolenaar | 1b5f03e | 2023-01-09 20:12:45 +0000 | [diff] [blame] | 750 | < *E1344* |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 751 | An interface can declare object variables, just like in a class but without |
| 752 | any initializer. |
Bram Moolenaar | 1b5f03e | 2023-01-09 20:12:45 +0000 | [diff] [blame] | 753 | *E1345* |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 754 | An interface can declare methods with `:def`, including the arguments and |
| 755 | return type, but without the body and without `:enddef`. Example: > |
| 756 | |
| 757 | interface HasSurface |
Doug Kearns | 74da0ee | 2023-12-14 20:26:26 +0100 | [diff] [blame] | 758 | var size: number |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 759 | def Surface(): number |
| 760 | endinterface |
| 761 | |
Bram Moolenaar | 1b5f03e | 2023-01-09 20:12:45 +0000 | [diff] [blame] | 762 | An interface name must start with an uppercase letter. *E1343* |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 763 | The "Has" prefix can be used to make it easier to guess this is an interface |
| 764 | name, with a hint about what it provides. |
Bram Moolenaar | 1b5f03e | 2023-01-09 20:12:45 +0000 | [diff] [blame] | 765 | An interface can only be defined in a |Vim9| script file. *E1342* |
Yegappan Lakshmanan | 00cd182 | 2023-09-18 19:56:49 +0200 | [diff] [blame] | 766 | An interface cannot "implement" another interface but it can "extend" another |
| 767 | interface. *E1381* |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 768 | |
| 769 | |
Bram Moolenaar | 938ae28 | 2023-02-20 20:44:55 +0000 | [diff] [blame] | 770 | null object ~ |
| 771 | |
Bram Moolenaar | dd60c36 | 2023-02-27 15:49:53 +0000 | [diff] [blame] | 772 | When a variable is declared to have the type of an object, but it is not |
Bram Moolenaar | 938ae28 | 2023-02-20 20:44:55 +0000 | [diff] [blame] | 773 | initialized, the value is null. When trying to use this null object Vim often |
| 774 | does not know what class was supposed to be used. Vim then cannot check if |
Aliaksei Budavei | b214995 | 2024-01-07 17:52:10 +0300 | [diff] [blame] | 775 | a variable name is correct and you will get a "Using a null object" error, |
h_east | ba77bbb | 2023-10-03 04:47:13 +0900 | [diff] [blame] | 776 | even when the variable name is invalid. *E1360* *E1362* |
Bram Moolenaar | 938ae28 | 2023-02-20 20:44:55 +0000 | [diff] [blame] | 777 | |
| 778 | |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 779 | Default constructor ~ |
Yegappan Lakshmanan | 413f839 | 2023-09-28 22:46:37 +0200 | [diff] [blame] | 780 | *default-constructor* |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 781 | In case you define a class without a new() method, one will be automatically |
| 782 | defined. This default constructor will have arguments for all the object |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 783 | variables, in the order they were specified. Thus if your class looks like: > |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 784 | |
| 785 | class AutoNew |
Doug Kearns | 74da0ee | 2023-12-14 20:26:26 +0100 | [diff] [blame] | 786 | var name: string |
| 787 | var age: number |
| 788 | var gender: Gender |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 789 | endclass |
| 790 | |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 791 | Then the default constructor will be: > |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 792 | |
Bram Moolenaar | 65b0d16 | 2022-12-13 18:43:22 +0000 | [diff] [blame] | 793 | def new(this.name = v:none, this.age = v:none, this.gender = v:none) |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 794 | enddef |
| 795 | |
Bram Moolenaar | 65b0d16 | 2022-12-13 18:43:22 +0000 | [diff] [blame] | 796 | The "= v:none" default values make the arguments optional. Thus you can also |
| 797 | call `new()` without any arguments. No assignment will happen and the default |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 798 | value for the object variables will be used. This is a more useful example, |
Bram Moolenaar | 65b0d16 | 2022-12-13 18:43:22 +0000 | [diff] [blame] | 799 | with default values: > |
Bram Moolenaar | 7db29e4 | 2022-12-11 15:53:04 +0000 | [diff] [blame] | 800 | |
| 801 | class TextPosition |
Doug Kearns | 74da0ee | 2023-12-14 20:26:26 +0100 | [diff] [blame] | 802 | var lnum: number = 1 |
| 803 | var col: number = 1 |
Bram Moolenaar | 7db29e4 | 2022-12-11 15:53:04 +0000 | [diff] [blame] | 804 | endclass |
| 805 | |
| 806 | If you want the constructor to have mandatory arguments, you need to write it |
| 807 | yourself. For example, if for the AutoNew class above you insist on getting |
| 808 | the name, you can define the constructor like this: > |
| 809 | |
Bram Moolenaar | 65b0d16 | 2022-12-13 18:43:22 +0000 | [diff] [blame] | 810 | def new(this.name, this.age = v:none, this.gender = v:none) |
Bram Moolenaar | 7db29e4 | 2022-12-11 15:53:04 +0000 | [diff] [blame] | 811 | enddef |
Yegappan Lakshmanan | 563e644 | 2023-12-05 08:19:06 -0800 | [diff] [blame] | 812 | < |
| 813 | When using the default new() method, if the order of the object variables in |
| 814 | the class is changed later, then all the callers of the default new() method |
Aliaksei Budavei | b214995 | 2024-01-07 17:52:10 +0300 | [diff] [blame] | 815 | need to change. To avoid this, the new() method can be explicitly defined |
Yegappan Lakshmanan | 563e644 | 2023-12-05 08:19:06 -0800 | [diff] [blame] | 816 | without any arguments. |
| 817 | |
| 818 | *E1328* |
Bram Moolenaar | 65b0d16 | 2022-12-13 18:43:22 +0000 | [diff] [blame] | 819 | Note that you cannot use another default value than "v:none" here. If you |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 820 | want to initialize the object variables, do it where they are declared. This |
Bram Moolenaar | 65b0d16 | 2022-12-13 18:43:22 +0000 | [diff] [blame] | 821 | way you only need to look in one place for the default values. |
Bram Moolenaar | 7db29e4 | 2022-12-11 15:53:04 +0000 | [diff] [blame] | 822 | |
Ernie Rael | 03042a2 | 2023-11-11 08:53:32 +0100 | [diff] [blame] | 823 | All object variables will be used in the default constructor, including |
| 824 | protected access ones. |
Bram Moolenaar | 1b5f03e | 2023-01-09 20:12:45 +0000 | [diff] [blame] | 825 | |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 826 | If the class extends another one, the object variables of that class will come |
Bram Moolenaar | 1b5f03e | 2023-01-09 20:12:45 +0000 | [diff] [blame] | 827 | first. |
| 828 | |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 829 | |
| 830 | Multiple constructors ~ |
errael | 92feeaf | 2023-12-27 10:07:09 -0800 | [diff] [blame] | 831 | *multiple-constructors* |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 832 | Normally a class has just one new() constructor. In case you find that the |
| 833 | constructor is often called with the same arguments you may want to simplify |
| 834 | your code by putting those arguments into a second constructor method. For |
| 835 | example, if you tend to use the color black a lot: > |
| 836 | |
| 837 | def new(this.garment, this.color, this.size) |
| 838 | enddef |
| 839 | ... |
| 840 | var pants = new(Garment.pants, Color.black, "XL") |
| 841 | var shirt = new(Garment.shirt, Color.black, "XL") |
| 842 | var shoes = new(Garment.shoes, Color.black, "45") |
| 843 | |
| 844 | Instead of repeating the color every time you can add a constructor that |
| 845 | includes it: > |
| 846 | |
| 847 | def newBlack(this.garment, this.size) |
| 848 | this.color = Color.black |
| 849 | enddef |
| 850 | ... |
| 851 | var pants = newBlack(Garment.pants, "XL") |
| 852 | var shirt = newBlack(Garment.shirt, "XL") |
| 853 | var shoes = newBlack(Garment.shoes, "9.5") |
| 854 | |
| 855 | Note that the method name must start with "new". If there is no method called |
| 856 | "new()" then the default constructor is added, even though there are other |
| 857 | constructor methods. |
| 858 | |
Yegappan Lakshmanan | 4f32c83 | 2024-01-12 17:36:40 +0100 | [diff] [blame] | 859 | Compiling methods in a Class ~ |
| 860 | *class-compile* |
| 861 | The |:defcompile| command can be used to compile all the class and object |
| 862 | methods defined in a class: > |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 863 | |
Yegappan Lakshmanan | 4f32c83 | 2024-01-12 17:36:40 +0100 | [diff] [blame] | 864 | defcompile MyClass # Compile class "MyClass" |
| 865 | defcompile # Compile the classes in the current script |
| 866 | < |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 867 | ============================================================================== |
| 868 | |
Christian Brabandt | 6c1afa3 | 2024-01-01 20:50:51 +0100 | [diff] [blame] | 869 | 7. Type definition *typealias* *Vim9-type* *:type* |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 870 | |
Yegappan Lakshmanan | 2a71b54 | 2023-12-14 20:03:03 +0100 | [diff] [blame] | 871 | *E1393* *E1395* *E1396* *E1397* *E1398* |
| 872 | A type definition is giving a name to a type specification. This is also |
| 873 | known as a "type alias". The type alias can be used wherever a built-in type |
| 874 | can be used. Example: > |
Yegappan Lakshmanan | 26e8f7b | 2023-10-06 10:24:10 -0700 | [diff] [blame] | 875 | |
Yegappan Lakshmanan | 2a71b54 | 2023-12-14 20:03:03 +0100 | [diff] [blame] | 876 | type ListOfStrings = list<string> |
| 877 | var s: ListOfStrings = ['a', 'b'] |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 878 | |
Yegappan Lakshmanan | 2a71b54 | 2023-12-14 20:03:03 +0100 | [diff] [blame] | 879 | def ProcessStr(str: ListOfStrings): ListOfStrings |
| 880 | return str |
| 881 | enddef |
| 882 | echo ProcessStr(s) |
| 883 | < |
| 884 | *E1394* |
| 885 | A type alias name must start with an upper case character. Only existing |
| 886 | types can be aliased. |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 887 | |
Yegappan Lakshmanan | 2a71b54 | 2023-12-14 20:03:03 +0100 | [diff] [blame] | 888 | *E1399* |
| 889 | A type alias can be created only at the script level and not inside a |
| 890 | function. A type alias can be exported and used across scripts. |
| 891 | |
| 892 | *E1400* *E1401* *E1402* *E1403* *E1407* |
| 893 | A type alias cannot be used as an expression. A type alias cannot be used in |
| 894 | the left-hand-side of an assignment. |
| 895 | |
| 896 | For a type alias name, the |typename()| function returns the type that is |
| 897 | aliased: > |
| 898 | |
| 899 | type ListOfStudents = list<dict<any>> |
| 900 | echo typename(ListOfStudents) |
| 901 | typealias<list<dict<any>>> |
| 902 | < |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 903 | ============================================================================== |
| 904 | |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 905 | 8. Enum *Vim9-enum* *:enum* *:endenum* |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 906 | |
Yegappan Lakshmanan | 26e8f7b | 2023-10-06 10:24:10 -0700 | [diff] [blame] | 907 | {not implemented yet} |
| 908 | |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 909 | An enum is a type that can have one of a list of values. Example: > |
| 910 | |
| 911 | :enum Color |
| 912 | White |
| 913 | Red |
| 914 | Green |
| 915 | Blue |
| 916 | Black |
| 917 | :endenum |
| 918 | |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 919 | |
| 920 | ============================================================================== |
| 921 | |
| 922 | 9. Rationale |
| 923 | |
| 924 | Most of the choices for |Vim9| classes come from popular and recently |
| 925 | developed languages, such as Java, TypeScript and Dart. The syntax has been |
| 926 | made to fit with the way Vim script works, such as using `endclass` instead of |
| 927 | using curly braces around the whole class. |
| 928 | |
| 929 | Some common constructs of object-oriented languages were chosen very long ago |
| 930 | when this kind of programming was still new, and later found to be |
| 931 | sub-optimal. By this time those constructs were widely used and changing them |
| 932 | was not an option. In Vim we do have the freedom to make different choices, |
| 933 | since classes are completely new. We can make the syntax simpler and more |
| 934 | consistent than what "old" languages use. Without diverting too much, it |
| 935 | should still mostly look like what you know from existing languages. |
| 936 | |
| 937 | Some recently developed languages add all kinds of fancy features that we |
| 938 | don't need for Vim. But some have nice ideas that we do want to use. |
| 939 | Thus we end up with a base of what is common in popular languages, dropping |
| 940 | what looks like a bad idea, and adding some nice features that are easy to |
| 941 | understand. |
| 942 | |
| 943 | The main rules we use to make decisions: |
| 944 | - Keep it simple. |
| 945 | - No surprises, mostly do what other languages are doing. |
| 946 | - Avoid mistakes from the past. |
| 947 | - Avoid the need for the script writer to consult the help to understand how |
| 948 | things work, most things should be obvious. |
| 949 | - Keep it consistent. |
| 950 | - Aim at an average size plugin, not at a huge project. |
| 951 | |
| 952 | |
| 953 | Using new() for the constructor ~ |
| 954 | |
| 955 | Many languages use the class name for the constructor method. A disadvantage |
| 956 | is that quite often this is a long name. And when changing the class name all |
| 957 | constructor methods need to be renamed. Not a big deal, but still a |
| 958 | disadvantage. |
| 959 | |
| 960 | Other languages, such as TypeScript, use a specific name, such as |
| 961 | "constructor()". That seems better. However, using "new" or "new()" to |
| 962 | create a new object has no obvious relation with "constructor()". |
| 963 | |
| 964 | For |Vim9| script using the same method name for all constructors seemed like |
| 965 | the right choice, and by calling it new() the relation between the caller and |
| 966 | the method being called is obvious. |
| 967 | |
| 968 | |
| 969 | No overloading of the constructor ~ |
| 970 | |
| 971 | In Vim script, both legacy and |Vim9| script, there is no overloading of |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 972 | methods. That means it is not possible to use the same method name with |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 973 | different types of arguments. Therefore there also is only one new() |
| 974 | constructor. |
| 975 | |
| 976 | With |Vim9| script it would be possible to support overloading, since |
| 977 | arguments are typed. However, this gets complicated very quickly. Looking at |
| 978 | a new() call one has to inspect the types of the arguments to know which of |
| 979 | several new() methods is actually being called. And that can require |
| 980 | inspecting quite a bit of code. For example, if one of the arguments is the |
| 981 | return value of a method, you need to find that method to see what type it is |
| 982 | returning. |
| 983 | |
| 984 | Instead, every constructor has to have a different name, starting with "new". |
| 985 | That way multiple constructors with different arguments are possible, while it |
| 986 | is very easy to see which constructor is being used. And the type of |
| 987 | arguments can be properly checked. |
| 988 | |
| 989 | |
| 990 | No overloading of methods ~ |
| 991 | |
| 992 | Same reasoning as for the constructor: It is often not obvious what type |
| 993 | arguments have, which would make it difficult to figure out what method is |
| 994 | actually being called. Better just give the methods a different name, then |
| 995 | type checking will make sure it works as you intended. This rules out |
| 996 | polymorphism, which we don't really need anyway. |
| 997 | |
| 998 | |
Bram Moolenaar | 1b5f03e | 2023-01-09 20:12:45 +0000 | [diff] [blame] | 999 | Single inheritance and interfaces ~ |
| 1000 | |
| 1001 | Some languages support multiple inheritance. Although that can be useful in |
| 1002 | some cases, it makes the rules of how a class works quite complicated. |
| 1003 | Instead, using interfaces to declare what is supported is much simpler. The |
| 1004 | very popular Java language does it this way, and it should be good enough for |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 1005 | Vim. The "keep it simple" rule applies here. |
Bram Moolenaar | 1b5f03e | 2023-01-09 20:12:45 +0000 | [diff] [blame] | 1006 | |
| 1007 | Explicitly declaring that a class supports an interface makes it easy to see |
| 1008 | what a class is intended for. It also makes it possible to do proper type |
| 1009 | checking. When an interface is changed any class that declares to implement |
| 1010 | it will be checked if that change was also changed. The mechanism to assume a |
| 1011 | class implements an interface just because the methods happen to match is |
| 1012 | brittle and leads to obscure problems, let's not do that. |
| 1013 | |
| 1014 | |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 1015 | Using "this.variable" everywhere ~ |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 1016 | |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 1017 | The object variables in various programming languages can often be accessed in |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 1018 | different ways, depending on the location. Sometimes "this." has to be |
| 1019 | prepended to avoid ambiguity. They are usually declared without "this.". |
| 1020 | That is quite inconsistent and sometimes confusing. |
| 1021 | |
| 1022 | A very common issue is that in the constructor the arguments use the same name |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 1023 | as the object variable. Then for these variables "this." needs to be prefixed |
| 1024 | in the body, while for other variables this is not needed and often omitted. |
| 1025 | This leads to a mix of variables with and without "this.", which is |
| 1026 | inconsistent. |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 1027 | |
Aliaksei Budavei | b214995 | 2024-01-07 17:52:10 +0300 | [diff] [blame] | 1028 | For |Vim9| classes the "this." prefix is always used for declared methods and |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 1029 | variables. Simple and consistent. When looking at the code inside a class |
| 1030 | it's also directly clear which variable references are object variables and |
| 1031 | which aren't. |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 1032 | |
| 1033 | |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 1034 | Using class variables ~ |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 1035 | |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 1036 | Using "static variable" to declare a class variable is very common, nothing |
| 1037 | new here. In |Vim9| script these can be accessed directly by their name. |
| 1038 | Very much like how a script-local variable can be used in a method. Since |
| 1039 | object variables are always accessed with "this." prepended, it's also quickly |
| 1040 | clear what kind of variable it is. |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 1041 | |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 1042 | TypeScript prepends the class name before the class variable name, also inside |
| 1043 | the class. This has two problems: The class name can be rather long, taking |
| 1044 | up quite a bit of space, and when the class is renamed all these places need |
| 1045 | to be changed too. |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 1046 | |
| 1047 | |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 1048 | Declaring object and class variables ~ |
Bram Moolenaar | 1b5f03e | 2023-01-09 20:12:45 +0000 | [diff] [blame] | 1049 | |
| 1050 | The main choice is whether to use "var" as with variable declarations. |
| 1051 | TypeScript does not use it: > |
| 1052 | class Point { |
| 1053 | x: number; |
| 1054 | y = 0; |
| 1055 | } |
| 1056 | |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 1057 | Following that Vim object variables could be declared like this: > |
Bram Moolenaar | 1b5f03e | 2023-01-09 20:12:45 +0000 | [diff] [blame] | 1058 | class Point |
| 1059 | this.x: number |
| 1060 | this.y = 0 |
| 1061 | endclass |
| 1062 | |
| 1063 | Some users pointed out that this looks more like an assignment than a |
Aliaksei Budavei | b214995 | 2024-01-07 17:52:10 +0300 | [diff] [blame] | 1064 | declaration. Adding "var" and omitting "this." changes that: > |
Bram Moolenaar | 1b5f03e | 2023-01-09 20:12:45 +0000 | [diff] [blame] | 1065 | class Point |
Doug Kearns | 74da0ee | 2023-12-14 20:26:26 +0100 | [diff] [blame] | 1066 | var x: number |
| 1067 | var y = 0 |
Bram Moolenaar | 1b5f03e | 2023-01-09 20:12:45 +0000 | [diff] [blame] | 1068 | endclass |
| 1069 | |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 1070 | We also need to be able to declare class variables using the "static" keyword. |
Bram Moolenaar | 1b5f03e | 2023-01-09 20:12:45 +0000 | [diff] [blame] | 1071 | There we can also choose to leave out "var": > |
| 1072 | class Point |
Doug Kearns | 74da0ee | 2023-12-14 20:26:26 +0100 | [diff] [blame] | 1073 | var x: number |
Bram Moolenaar | 1b5f03e | 2023-01-09 20:12:45 +0000 | [diff] [blame] | 1074 | static count = 0 |
| 1075 | endclass |
| 1076 | |
| 1077 | Or do use it, before "static": > |
| 1078 | class Point |
Doug Kearns | 74da0ee | 2023-12-14 20:26:26 +0100 | [diff] [blame] | 1079 | var x: number |
Bram Moolenaar | 1b5f03e | 2023-01-09 20:12:45 +0000 | [diff] [blame] | 1080 | var static count = 0 |
| 1081 | endclass |
| 1082 | |
| 1083 | Or after "static": > |
| 1084 | class Point |
Doug Kearns | 74da0ee | 2023-12-14 20:26:26 +0100 | [diff] [blame] | 1085 | var x: number |
Bram Moolenaar | 1b5f03e | 2023-01-09 20:12:45 +0000 | [diff] [blame] | 1086 | static var count = 0 |
| 1087 | endclass |
| 1088 | |
| 1089 | This is more in line with "static def Func()". |
| 1090 | |
| 1091 | There is no clear preference whether to use "var" or not. The two main |
| 1092 | reasons to leave it out are: |
Doug Kearns | 74da0ee | 2023-12-14 20:26:26 +0100 | [diff] [blame] | 1093 | 1. TypeScript and other popular languages do not use it. |
Bram Moolenaar | 1b5f03e | 2023-01-09 20:12:45 +0000 | [diff] [blame] | 1094 | 2. Less clutter. |
| 1095 | |
Doug Kearns | 74da0ee | 2023-12-14 20:26:26 +0100 | [diff] [blame] | 1096 | However, it is more common for languages to reuse their general variable and |
| 1097 | function declaration syntax for class/object variables and methods. Vim9 also |
| 1098 | reuses the general function declaration syntax for methods. So, for the sake |
| 1099 | of consistency, we require "var" in these declarations. |
| 1100 | |
Bram Moolenaar | 1b5f03e | 2023-01-09 20:12:45 +0000 | [diff] [blame] | 1101 | |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 1102 | Using "ClassName.new()" to construct an object ~ |
| 1103 | |
| 1104 | Many languages use the "new" operator to create an object, which is actually |
| 1105 | kind of strange, since the constructor is defined as a method with arguments, |
| 1106 | not a command. TypeScript also has the "new" keyword, but the method is |
| 1107 | called "constructor()", it is hard to see the relation between the two. |
| 1108 | |
| 1109 | In |Vim9| script the constructor method is called new(), and it is invoked as |
| 1110 | new(), simple and straightforward. Other languages use "new ClassName()", |
| 1111 | while there is no ClassName() method, it's a method by another name in the |
| 1112 | class called ClassName. Quite confusing. |
| 1113 | |
| 1114 | |
Ernie Rael | 03042a2 | 2023-11-11 08:53:32 +0100 | [diff] [blame] | 1115 | Vim9class access modes ~ |
| 1116 | *vim9-access-modes* |
| 1117 | The variable access modes, and their meaning, supported by Vim9class are |
| 1118 | |public-variable| read and write from anywhere |
| 1119 | |read-only-variable| read from anywhere, write from inside the |
| 1120 | class and sub-classes |
| 1121 | |protected-variable| read and write from inside the class and |
| 1122 | sub-classes |
| 1123 | |
| 1124 | The method access modes are similar, but without the read-only mode. |
| 1125 | |
| 1126 | |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 1127 | Default read access to object variables ~ |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 1128 | |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 1129 | Some users will remark that the access rules for object variables are |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 1130 | asymmetric. Well, that is intentional. Changing a value is a very different |
| 1131 | action than reading a value. The read operation has no side effects, it can |
| 1132 | be done any number of times without affecting the object. Changing the value |
| 1133 | can have many side effects, and even have a ripple effect, affecting other |
| 1134 | objects. |
| 1135 | |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 1136 | When adding object variables one usually doesn't think much about this, just |
| 1137 | get the type right. And normally the values are set in the new() method. |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 1138 | Therefore defaulting to read access only "just works" in most cases. And when |
| 1139 | directly writing you get an error, which makes you wonder if you actually want |
| 1140 | to allow that. This helps writing code with fewer mistakes. |
| 1141 | |
| 1142 | |
Ernie Rael | 03042a2 | 2023-11-11 08:53:32 +0100 | [diff] [blame] | 1143 | Making object variables protected with an underscore ~ |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 1144 | |
Ernie Rael | 03042a2 | 2023-11-11 08:53:32 +0100 | [diff] [blame] | 1145 | When an object variable is protected, it can only be read and changed inside |
| 1146 | the class (and in sub-classes), then it cannot be used outside of the class. |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 1147 | Prepending an underscore is a simple way to make that visible. Various |
| 1148 | programming languages have this as a recommendation. |
| 1149 | |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 1150 | In case you change your mind and want to make the object variable accessible |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 1151 | outside of the class, you will have to remove the underscore everywhere. |
| 1152 | Since the name only appears in the class (and sub-classes) they will be easy |
| 1153 | to find and change. |
| 1154 | |
| 1155 | The other way around is much harder: you can easily prepend an underscore to |
Ernie Rael | 03042a2 | 2023-11-11 08:53:32 +0100 | [diff] [blame] | 1156 | the object variable inside the class to make it protected, but any usage |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 1157 | elsewhere you will have to track down and change. You may have to make it a |
| 1158 | "set" method call. This reflects the real world problem that taking away |
| 1159 | access requires work to be done for all places where that access exists. |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 1160 | |
Ernie Rael | 03042a2 | 2023-11-11 08:53:32 +0100 | [diff] [blame] | 1161 | An alternative would have been using the "protected" keyword, just like |
| 1162 | "public" changes the access in the other direction. Well, that's just to |
| 1163 | reduce the number of keywords. |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 1164 | |
| 1165 | |
Ernie Rael | 03042a2 | 2023-11-11 08:53:32 +0100 | [diff] [blame] | 1166 | No private object variables ~ |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 1167 | |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 1168 | Some languages provide several ways to control access to object variables. |
| 1169 | The most known is "protected", and the meaning varies from language to |
Ernie Rael | 03042a2 | 2023-11-11 08:53:32 +0100 | [diff] [blame] | 1170 | language. Others are "shared", "private", "package" and even "friend". |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 1171 | |
| 1172 | These rules make life more difficult. That can be justified in projects where |
| 1173 | many people work on the same, complex code where it is easy to make mistakes. |
| 1174 | Especially when refactoring or other changes to the class model. |
| 1175 | |
| 1176 | The Vim scripts are expected to be used in a plugin, with just one person or a |
| 1177 | small team working on it. Complex rules then only make it more complicated, |
Bram Moolenaar | 71badf9 | 2023-04-22 22:40:14 +0100 | [diff] [blame] | 1178 | the extra safety provided by the rules isn't really needed. Let's just keep |
| 1179 | it simple and not specify access details. |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 1180 | |
| 1181 | |
| 1182 | ============================================================================== |
| 1183 | |
| 1184 | 10. To be done later |
| 1185 | |
| 1186 | Can a newSomething() constructor invoke another constructor? If yes, what are |
| 1187 | the restrictions? |
| 1188 | |
| 1189 | Thoughts: |
| 1190 | - Generics for a class: `class <Tkey, Tentry>` |
| 1191 | - Generics for a function: `def <Tkey> GetLast(key: Tkey)` |
| 1192 | - Mixins: not sure if that is useful, leave out for simplicity. |
| 1193 | |
| 1194 | Some things that look like good additions: |
| 1195 | - For testing: Mock mechanism |
| 1196 | |
| 1197 | An important class to be provided is "Promise". Since Vim is single |
| 1198 | threaded, connecting asynchronous operations is a natural way of allowing |
| 1199 | plugins to do their work without blocking the user. It's a uniform way to |
| 1200 | invoke callbacks and handle timeouts and errors. |
| 1201 | |
| 1202 | |
| 1203 | vim:tw=78:ts=8:noet:ft=help:norl: |