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