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