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