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