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