Hirohito Higashi | 31b78cc | 2025-04-21 19:39:15 +0200 | [diff] [blame] | 1 | *vim9class.txt* For Vim version 9.1. Last change: 2025 Apr 21 |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 2 | |
| 3 | |
| 4 | VIM REFERENCE MANUAL by Bram Moolenaar |
| 5 | |
| 6 | |
Yegappan Lakshmanan | 49cdd62 | 2023-12-24 11:01:23 +0100 | [diff] [blame] | 7 | Vim9 classes, objects, interfaces, types and enums. *vim9-class* |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 8 | |
| 9 | 1. Overview |Vim9-class-overview| |
| 10 | 2. A simple class |Vim9-simple-class| |
Yegappan Lakshmanan | 00cd182 | 2023-09-18 19:56:49 +0200 | [diff] [blame] | 11 | 3. Class variables and methods |Vim9-class-member| |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 12 | 4. Using an abstract class |Vim9-abstract-class| |
| 13 | 5. Using an interface |Vim9-using-interface| |
| 14 | 6. More class details |Vim9-class| |
| 15 | 7. Type definition |Vim9-type| |
| 16 | 8. Enum |Vim9-enum| |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 17 | |
| 18 | 9. Rationale |
| 19 | 10. To be done later |
| 20 | |
| 21 | ============================================================================== |
| 22 | |
| 23 | 1. Overview *Vim9-class-overview* |
| 24 | |
| 25 | 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] | 26 | material on this subject. Here we document what |Vim9| script provides, |
| 27 | assuming you know the basics already. Added are helpful hints about how to |
Yegappan Lakshmanan | 0ab500d | 2023-10-21 11:59:42 +0200 | [diff] [blame] | 28 | use this functionality effectively. Vim9 classes and objects cannot be used |
| 29 | in legacy Vim scripts and legacy functions. |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 30 | |
| 31 | The basic item is an object: |
| 32 | - An object stores state. It contains one or more variables that can each |
| 33 | have a value. |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 34 | - An object provides functions that use and manipulate its state. These |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 35 | functions are invoked "on the object", which is what sets it apart from the |
| 36 | traditional separation of data and code that manipulates the data. |
| 37 | - An object has a well defined interface, with typed member variables and |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 38 | methods. |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 39 | - Objects are created from a class and all objects have the same interface. |
| 40 | This does not change at runtime, it is not dynamic. |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 41 | |
| 42 | An object can only be created by a class. A class provides: |
| 43 | - A new() method, the constructor, which returns an object for the class. |
| 44 | This method is invoked on the class name: MyClass.new(). |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 45 | - State shared by all objects of the class: class variables (class members). |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 46 | - A hierarchy of classes, with super-classes and sub-classes, inheritance. |
| 47 | |
| 48 | An interface is used to specify properties of an object: |
| 49 | - An object can declare several interfaces that it implements. |
| 50 | - Different objects implementing the same interface can be used the same way. |
| 51 | |
| 52 | The class hierarchy allows for single inheritance. Otherwise interfaces are |
| 53 | to be used where needed. |
| 54 | |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 55 | Class modeling ~ |
| 56 | |
| 57 | You can model classes any way you like. Keep in mind what you are building, |
| 58 | don't try to model the real world. This can be confusing, especially because |
| 59 | teachers use real-world objects to explain class relations and you might think |
| 60 | your model should therefore reflect the real world. It doesn't! The model |
| 61 | should match your purpose. |
| 62 | |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 63 | Keep in mind that composition (an object contains other objects) is often |
| 64 | better than inheritance (an object extends another object). Don't waste time |
| 65 | trying to find the optimal class model. Or waste time discussing whether a |
| 66 | 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] | 67 | |
| 68 | |
| 69 | ============================================================================== |
| 70 | |
| 71 | 2. A simple class *Vim9-simple-class* |
| 72 | |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 73 | Let's start with a simple example: a class that stores a text position (see |
| 74 | below for how to do this more efficiently): > |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 75 | |
| 76 | class TextPosition |
Doug Kearns | 74da0ee | 2023-12-14 20:26:26 +0100 | [diff] [blame] | 77 | var lnum: number |
| 78 | var col: number |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 79 | |
| 80 | def new(lnum: number, col: number) |
| 81 | this.lnum = lnum |
| 82 | this.col = col |
| 83 | enddef |
| 84 | |
| 85 | def SetLnum(lnum: number) |
| 86 | this.lnum = lnum |
| 87 | enddef |
| 88 | |
| 89 | def SetCol(col: number) |
| 90 | this.col = col |
| 91 | enddef |
| 92 | |
| 93 | def SetPosition(lnum: number, col: number) |
| 94 | this.lnum = lnum |
| 95 | this.col = col |
| 96 | enddef |
| 97 | endclass |
Bram Moolenaar | 7db29e4 | 2022-12-11 15:53:04 +0000 | [diff] [blame] | 98 | < *object* *Object* |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 99 | You can create an object from this class with the new() method: > |
| 100 | |
| 101 | var pos = TextPosition.new(1, 1) |
errael | 1d4fcfe | 2023-12-21 08:34:15 -0800 | [diff] [blame] | 102 | < |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 103 | The object variables "lnum" and "col" can be accessed directly: > |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 104 | |
| 105 | echo $'The text position is ({pos.lnum}, {pos.col})' |
Christian Brabandt | 6c1afa3 | 2024-01-01 20:50:51 +0100 | [diff] [blame] | 106 | < *E1317* *E1327* *:this* |
errael | 1d4fcfe | 2023-12-21 08:34:15 -0800 | [diff] [blame] | 107 | If you have been using other object-oriented languages you will notice that in |
Aliaksei Budavei | b214995 | 2024-01-07 17:52:10 +0300 | [diff] [blame] | 108 | Vim, within a class definition, the declared object members are consistently |
| 109 | referred to with the "this." prefix. This is different from languages like |
| 110 | Java and TypeScript. The naming convention makes the object members easy to |
| 111 | spot. Also, when a variable does not have the "this." prefix you know it is |
| 112 | not an object variable. |
errael | 1d4fcfe | 2023-12-21 08:34:15 -0800 | [diff] [blame] | 113 | *E1411* |
| 114 | From outside the class definition, access an object's methods and variables by |
| 115 | using the object name followed by a dot following by the member: > |
| 116 | |
| 117 | pos.lnum |
Aliaksei Budavei | b214995 | 2024-01-07 17:52:10 +0300 | [diff] [blame] | 118 | pos.SetCol(10) |
errael | 1d4fcfe | 2023-12-21 08:34:15 -0800 | [diff] [blame] | 119 | < |
| 120 | *E1405* *E1406* |
| 121 | A class name cannot be used as an expression. A class name cannot be used in |
| 122 | the left-hand-side of an assignment. |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 123 | |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 124 | Object variable write access ~ |
Ernie Rael | 03042a2 | 2023-11-11 08:53:32 +0100 | [diff] [blame] | 125 | *read-only-variable* |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 126 | Now try to change an object variable directly: > |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 127 | |
| 128 | pos.lnum = 9 |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 129 | < *E1335* |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 130 | This will give you an error! That is because by default object variables can |
| 131 | be read but not set. That's why the TextPosition class provides a method for |
| 132 | it: > |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 133 | |
| 134 | pos.SetLnum(9) |
| 135 | |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 136 | Allowing to read but not set an object variable is the most common and safest |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 137 | way. Most often there is no problem using a value, while setting a value may |
| 138 | have side effects that need to be taken care of. In this case, the SetLnum() |
| 139 | method could check if the line number is valid and either give an error or use |
| 140 | the closest valid value. |
Ernie Rael | 03042a2 | 2023-11-11 08:53:32 +0100 | [diff] [blame] | 141 | *:public* *public-variable* *E1331* |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 142 | If you don't care about side effects and want to allow the object variable to |
| 143 | be changed at any time, you can make it public: > |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 144 | |
Aliaksei Budavei | 0aed99a | 2023-12-29 01:08:24 +0300 | [diff] [blame] | 145 | public var lnum: number |
| 146 | public var col: number |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 147 | |
| 148 | Now you don't need the SetLnum(), SetCol() and SetPosition() methods, setting |
| 149 | "pos.lnum" directly above will no longer give an error. |
Yegappan Lakshmanan | 00cd182 | 2023-09-18 19:56:49 +0200 | [diff] [blame] | 150 | *E1326* |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 151 | If you try to set an object variable that doesn't exist you get an error: > |
Bram Moolenaar | f1dcd14 | 2022-12-31 15:30:45 +0000 | [diff] [blame] | 152 | pos.other = 9 |
Yegappan Lakshmanan | 00cd182 | 2023-09-18 19:56:49 +0200 | [diff] [blame] | 153 | < E1326: Member not found on object "TextPosition": other ~ |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 154 | |
Yegappan Lakshmanan | 00cd182 | 2023-09-18 19:56:49 +0200 | [diff] [blame] | 155 | *E1376* |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 156 | A object variable cannot be accessed using the class name. |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 157 | |
Ernie Rael | 03042a2 | 2023-11-11 08:53:32 +0100 | [diff] [blame] | 158 | Protected variables ~ |
| 159 | *protected-variable* *E1332* *E1333* |
| 160 | On the other hand, if you do not want the object variables to be read directly |
| 161 | from outside the class or its sub-classes, you can make them protected. This |
| 162 | is done by prefixing an underscore to the name: > |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 163 | |
Doug Kearns | 74da0ee | 2023-12-14 20:26:26 +0100 | [diff] [blame] | 164 | var _lnum: number |
Aliaksei Budavei | b214995 | 2024-01-07 17:52:10 +0300 | [diff] [blame] | 165 | var _col: number |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 166 | |
Ernie Rael | 03042a2 | 2023-11-11 08:53:32 +0100 | [diff] [blame] | 167 | Now you need to provide methods to get the value of the protected variables. |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 168 | 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] | 169 | "Get": > |
| 170 | |
| 171 | def GetLnum(): number |
| 172 | return this._lnum |
| 173 | enddef |
| 174 | |
Aliaksei Budavei | b214995 | 2024-01-07 17:52:10 +0300 | [diff] [blame] | 175 | def GetCol(): number |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 176 | return this._col |
| 177 | enddef |
| 178 | |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 179 | This example isn't very useful, the variables might as well have been public. |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 180 | It does become useful if you check the value. For example, restrict the line |
| 181 | number to the total number of lines: > |
| 182 | |
| 183 | def GetLnum(): number |
| 184 | if this._lnum > this._lineCount |
| 185 | return this._lineCount |
| 186 | endif |
| 187 | return this._lnum |
| 188 | enddef |
Yegappan Lakshmanan | 00cd182 | 2023-09-18 19:56:49 +0200 | [diff] [blame] | 189 | < |
Ernie Rael | 03042a2 | 2023-11-11 08:53:32 +0100 | [diff] [blame] | 190 | Protected methods ~ |
| 191 | *protected-method* *E1366* |
Yegappan Lakshmanan | cd7293b | 2023-08-27 19:18:23 +0200 | [diff] [blame] | 192 | If you want object methods to be accessible only from other methods of the |
| 193 | same class and not used from outside the class, then you can make them |
Ernie Rael | 03042a2 | 2023-11-11 08:53:32 +0100 | [diff] [blame] | 194 | protected. This is done by prefixing the method name with an underscore: > |
Yegappan Lakshmanan | cd7293b | 2023-08-27 19:18:23 +0200 | [diff] [blame] | 195 | |
| 196 | class SomeClass |
| 197 | def _Foo(): number |
| 198 | return 10 |
| 199 | enddef |
| 200 | def Bar(): number |
| 201 | return this._Foo() |
| 202 | enddef |
| 203 | endclass |
| 204 | < |
Ernie Rael | 03042a2 | 2023-11-11 08:53:32 +0100 | [diff] [blame] | 205 | Accessing a protected method outside the class will result in an error (using |
Yegappan Lakshmanan | cd7293b | 2023-08-27 19:18:23 +0200 | [diff] [blame] | 206 | the above class): > |
| 207 | |
| 208 | var a = SomeClass.new() |
| 209 | a._Foo() |
| 210 | < |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 211 | Simplifying the new() method ~ |
Yegappan Lakshmanan | 413f839 | 2023-09-28 22:46:37 +0200 | [diff] [blame] | 212 | *new()* *constructor* |
errael | 92feeaf | 2023-12-27 10:07:09 -0800 | [diff] [blame] | 213 | See also |default-constructor| and |multiple-constructors|. |
| 214 | |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 215 | Many constructors take values for the object variables. Thus you very often |
| 216 | see this pattern: > |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 217 | |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 218 | class SomeClass |
Doug Kearns | 74da0ee | 2023-12-14 20:26:26 +0100 | [diff] [blame] | 219 | var lnum: number |
| 220 | var col: number |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 221 | |
| 222 | def new(lnum: number, col: number) |
| 223 | this.lnum = lnum |
| 224 | this.col = col |
| 225 | enddef |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 226 | endclass |
h-east | db38552 | 2023-09-28 22:18:19 +0200 | [diff] [blame] | 227 | < |
| 228 | *E1390* |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 229 | Not only is this text you need to write, it also has the type of each |
Dominique Pellé | 17dca3c | 2023-12-14 20:36:32 +0100 | [diff] [blame] | 230 | variable twice. Since this is so common a shorter way to write new() is |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 231 | provided: > |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 232 | |
| 233 | def new(this.lnum, this.col) |
| 234 | enddef |
| 235 | |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 236 | The semantics are easy to understand: Providing the object variable name, |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 237 | including "this.", as the argument to new() means the value provided in the |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 238 | new() call is assigned to that object variable. This mechanism comes from the |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 239 | Dart language. |
| 240 | |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 241 | Putting together this way of using new() and making the variables public |
| 242 | results in a much shorter class definition than what we started with: > |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 243 | |
| 244 | class TextPosition |
Doug Kearns | 74da0ee | 2023-12-14 20:26:26 +0100 | [diff] [blame] | 245 | public var lnum: number |
| 246 | public var col: number |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 247 | |
| 248 | def new(this.lnum, this.col) |
| 249 | enddef |
| 250 | |
| 251 | def SetPosition(lnum: number, col: number) |
| 252 | this.lnum = lnum |
| 253 | this.col = col |
| 254 | enddef |
| 255 | endclass |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 256 | |
| 257 | The sequence of constructing a new object is: |
| 258 | 1. Memory is allocated and cleared. All values are zero/false/empty. |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 259 | 2. For each declared object variable that has an initializer, the expression |
| 260 | is evaluated and assigned to the variable. This happens in the sequence |
| 261 | the variables are declared in the class. |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 262 | 3. Arguments in the new() method in the "this.name" form are assigned. |
| 263 | 4. The body of the new() method is executed. |
| 264 | |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 265 | If the class extends a parent class, the same thing happens. In the second |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 266 | step the object variables of the parent class are initialized first. There is |
| 267 | no need to call "super()" or "new()" on the parent. |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 268 | |
Yegappan Lakshmanan | b90e3bc | 2023-09-28 23:06:48 +0200 | [diff] [blame] | 269 | *E1365* |
Yegappan Lakshmanan | 6ac1544 | 2023-08-20 18:20:17 +0200 | [diff] [blame] | 270 | When defining the new() method the return type should not be specified. It |
| 271 | always returns an object of the class. |
| 272 | |
Yegappan Lakshmanan | 7e89800 | 2025-02-11 22:07:05 +0100 | [diff] [blame] | 273 | The new() method can be made a protected method by using "_new()". This can |
| 274 | be used to support the singleton design pattern. |
| 275 | |
Yegappan Lakshmanan | b90e3bc | 2023-09-28 23:06:48 +0200 | [diff] [blame] | 276 | *E1386* |
| 277 | When invoking an object method, the method name should be preceded by the |
Dominique Pellé | 17dca3c | 2023-12-14 20:36:32 +0100 | [diff] [blame] | 278 | object variable name. An object method cannot be invoked using the class |
Yegappan Lakshmanan | b90e3bc | 2023-09-28 23:06:48 +0200 | [diff] [blame] | 279 | name. |
| 280 | |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 281 | ============================================================================== |
| 282 | |
Yegappan Lakshmanan | 00cd182 | 2023-09-18 19:56:49 +0200 | [diff] [blame] | 283 | 3. Class Variables and Methods *Vim9-class-member* |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 284 | |
Yegappan Lakshmanan | 00cd182 | 2023-09-18 19:56:49 +0200 | [diff] [blame] | 285 | *:static* *E1337* *E1338* *E1368* |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 286 | 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] | 287 | prefix in the class where they are defined: > |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 288 | |
| 289 | class OtherThing |
Doug Kearns | 74da0ee | 2023-12-14 20:26:26 +0100 | [diff] [blame] | 290 | var size: number |
| 291 | static var totalSize: number |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 292 | |
| 293 | def new(this.size) |
| 294 | totalSize += this.size |
| 295 | enddef |
| 296 | endclass |
| 297 | < *E1340* *E1341* |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 298 | Since the name is used as-is, shadowing the name by a method argument name |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 299 | or local variable name is not allowed. |
| 300 | |
Yegappan Lakshmanan | b90e3bc | 2023-09-28 23:06:48 +0200 | [diff] [blame] | 301 | *E1374* *E1375* *E1384* *E1385* |
Yegappan Lakshmanan | 00cd182 | 2023-09-18 19:56:49 +0200 | [diff] [blame] | 302 | To access a class member outside of the class where it is defined, the class |
| 303 | name prefix must be used. A class member cannot be accessed using an object. |
| 304 | |
Ernie Rael | 03042a2 | 2023-11-11 08:53:32 +0100 | [diff] [blame] | 305 | Just like object members the access can be made protected by using an |
| 306 | underscore as the first character in the name, and it can be made public by |
| 307 | prefixing "public": > |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 308 | |
| 309 | class OtherThing |
Doug Kearns | 74da0ee | 2023-12-14 20:26:26 +0100 | [diff] [blame] | 310 | static var total: number # anybody can read, only class can write |
| 311 | static var _sum: number # only class can read and write |
| 312 | public static var result: number # anybody can read and write |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 313 | endclass |
| 314 | < |
Yegappan Lakshmanan | 00cd182 | 2023-09-18 19:56:49 +0200 | [diff] [blame] | 315 | *class-method* |
| 316 | Class methods are also declared with "static". They can use the class |
| 317 | variables but they have no access to the object variables, they cannot use the |
h_east | ba77bbb | 2023-10-03 04:47:13 +0900 | [diff] [blame] | 318 | "this" keyword: |
Yegappan Lakshmanan | 00cd182 | 2023-09-18 19:56:49 +0200 | [diff] [blame] | 319 | > |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 320 | class OtherThing |
Doug Kearns | 74da0ee | 2023-12-14 20:26:26 +0100 | [diff] [blame] | 321 | var size: number |
| 322 | static var totalSize: number |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 323 | |
| 324 | # Clear the total size and return the value it had before. |
| 325 | static def ClearTotalSize(): number |
| 326 | var prev = totalSize |
| 327 | totalSize = 0 |
| 328 | return prev |
| 329 | enddef |
| 330 | endclass |
| 331 | |
Aliaksei Budavei | 9574022 | 2024-04-04 23:05:33 +0300 | [diff] [blame] | 332 | Inside the class, the class method can be called by name directly, outside the |
| 333 | class, the class name must be prefixed: `OtherThing.ClearTotalSize()`. Also, |
| 334 | the name prefix must be used for public class methods in the special contexts |
| 335 | of class variable initializers and of lambda expressions and nested functions: |
| 336 | > |
| 337 | class OtherThing |
| 338 | static var name: string = OtherThing.GiveName() |
| 339 | |
| 340 | static def GiveName(): string |
| 341 | def DoGiveName(): string |
| 342 | return OtherThing.NameAny() |
| 343 | enddef |
| 344 | |
| 345 | return DoGiveName() |
| 346 | enddef |
| 347 | |
| 348 | static def NameAny(): string |
| 349 | return "any" |
| 350 | enddef |
| 351 | endclass |
| 352 | < |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 353 | |
Ernie Rael | 03042a2 | 2023-11-11 08:53:32 +0100 | [diff] [blame] | 354 | Just like object methods the access can be made protected by using an |
| 355 | underscore as the first character in the method name: > |
Yegappan Lakshmanan | cd7293b | 2023-08-27 19:18:23 +0200 | [diff] [blame] | 356 | |
| 357 | class OtherThing |
| 358 | static def _Foo() |
| 359 | echo "Foo" |
| 360 | enddef |
| 361 | def Bar() |
Yegappan Lakshmanan | 00cd182 | 2023-09-18 19:56:49 +0200 | [diff] [blame] | 362 | _Foo() |
Yegappan Lakshmanan | cd7293b | 2023-08-27 19:18:23 +0200 | [diff] [blame] | 363 | enddef |
| 364 | endclass |
Gianmaria Bajo | 4b9777a | 2023-08-29 22:26:30 +0200 | [diff] [blame] | 365 | < |
| 366 | *E1370* |
Ernie Rael | 4e28631 | 2023-12-24 11:03:31 +0100 | [diff] [blame] | 367 | Note that constructors cannot be declared as "static". They are called like a |
| 368 | static but execute as an object method; they have access to "this". |
Yegappan Lakshmanan | cd7293b | 2023-08-27 19:18:23 +0200 | [diff] [blame] | 369 | |
Yegappan Lakshmanan | 00cd182 | 2023-09-18 19:56:49 +0200 | [diff] [blame] | 370 | To access the class methods and class variables of a super class in an |
| 371 | extended class, the class name prefix should be used just as from anywhere |
| 372 | outside of the defining class: > |
| 373 | |
| 374 | vim9script |
| 375 | class Vehicle |
Doug Kearns | 74da0ee | 2023-12-14 20:26:26 +0100 | [diff] [blame] | 376 | static var nextID: number = 1000 |
Yegappan Lakshmanan | 00cd182 | 2023-09-18 19:56:49 +0200 | [diff] [blame] | 377 | static def GetID(): number |
| 378 | nextID += 1 |
| 379 | return nextID |
| 380 | enddef |
| 381 | endclass |
| 382 | class Car extends Vehicle |
Doug Kearns | 74da0ee | 2023-12-14 20:26:26 +0100 | [diff] [blame] | 383 | var myID: number |
Yegappan Lakshmanan | 00cd182 | 2023-09-18 19:56:49 +0200 | [diff] [blame] | 384 | def new() |
| 385 | this.myID = Vehicle.GetID() |
| 386 | enddef |
| 387 | endclass |
| 388 | < |
| 389 | Class variables and methods are not inherited by a child class. A child class |
| 390 | can declare a static variable or a method with the same name as the one in the |
| 391 | super class. Depending on the class where the member is used the |
| 392 | corresponding class member will be used. The type of the class member in a |
| 393 | child class can be different from that in the super class. |
| 394 | |
Yegappan Lakshmanan | ff6f0d5 | 2023-12-21 16:46:18 +0100 | [diff] [blame] | 395 | The double underscore (__) prefix for a class or object method name is |
| 396 | reserved for future use. |
| 397 | |
Yegappan Lakshmanan | e5437c5 | 2023-12-16 14:11:19 +0100 | [diff] [blame] | 398 | *object-final-variable* *E1409* |
| 399 | The |:final| keyword can be used to make a class or object variable a |
| 400 | constant. Examples: > |
| 401 | |
| 402 | class A |
| 403 | final v1 = [1, 2] # final object variable |
| 404 | public final v2 = {x: 1} # final object variable |
| 405 | static final v3 = 'abc' # final class variable |
| 406 | public static final v4 = 0z10 # final class variable |
| 407 | endclass |
| 408 | < |
| 409 | A final variable can be changed only from a constructor function. Example: > |
| 410 | |
| 411 | class A |
| 412 | final v1: list<number> |
| 413 | def new() |
| 414 | this.v1 = [1, 2] |
| 415 | enddef |
| 416 | endclass |
| 417 | var a = A.new() |
| 418 | echo a.v1 |
| 419 | < |
| 420 | Note that the value of a final variable can be changed. Example: > |
| 421 | |
| 422 | class A |
| 423 | public final v1 = [1, 2] |
| 424 | endclass |
| 425 | var a = A.new() |
| 426 | a.v1[0] = 6 # OK |
| 427 | a.v1->add(3) # OK |
| 428 | a.v1 = [3, 4] # Error |
| 429 | < |
| 430 | *E1408* |
| 431 | Final variables are not supported in an interface. A class or object method |
| 432 | cannot be final. |
| 433 | |
| 434 | *object-const-variable* |
| 435 | The |:const| keyword can be used to make a class or object variable and the |
| 436 | value a constant. Examples: > |
| 437 | |
| 438 | class A |
| 439 | const v1 = [1, 2] # const object variable |
| 440 | public const v2 = {x: 1} # const object variable |
| 441 | static const v3 = 'abc' # const class variable |
| 442 | public static const v4 = 0z10 # const class variable |
| 443 | endclass |
| 444 | < |
| 445 | A const variable can be changed only from a constructor function. Example: > |
| 446 | |
| 447 | class A |
| 448 | const v1: list<number> |
| 449 | def new() |
| 450 | this.v1 = [1, 2] |
| 451 | enddef |
| 452 | endclass |
| 453 | var a = A.new() |
| 454 | echo a.v1 |
| 455 | < |
| 456 | A const variable and its value cannot be changed. Example: > |
| 457 | |
| 458 | class A |
| 459 | public const v1 = [1, 2] |
| 460 | endclass |
| 461 | var a = A.new() |
| 462 | a.v1[0] = 6 # Error |
| 463 | a.v1->add(3) # Error |
| 464 | a.v1 = [3, 4] # Error |
| 465 | < |
| 466 | *E1410* |
| 467 | Const variables are not supported in an interface. A class or object method |
| 468 | cannot be a const. |
| 469 | |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 470 | ============================================================================== |
| 471 | |
| 472 | 4. Using an abstract class *Vim9-abstract-class* |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 473 | |
| 474 | An abstract class forms the base for at least one sub-class. In the class |
| 475 | 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] | 476 | 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] | 477 | an object from. A sub-class must extend the abstract class and add the |
| 478 | missing state and/or methods before it can be used to create objects for. |
| 479 | |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 480 | For example, a Shape class could store a color and thickness. You cannot |
| 481 | create a Shape object, it is missing the information about what kind of shape |
| 482 | it is. The Shape class functions as the base for a Square and a Triangle |
| 483 | class, for which objects can be created. Example: > |
| 484 | |
| 485 | abstract class Shape |
Doug Kearns | 74da0ee | 2023-12-14 20:26:26 +0100 | [diff] [blame] | 486 | var color = Color.Black |
| 487 | var thickness = 10 |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 488 | endclass |
| 489 | |
| 490 | class Square extends Shape |
Doug Kearns | 74da0ee | 2023-12-14 20:26:26 +0100 | [diff] [blame] | 491 | var size: number |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 492 | |
| 493 | def new(this.size) |
| 494 | enddef |
| 495 | endclass |
| 496 | |
| 497 | class Triangle extends Shape |
Doug Kearns | 74da0ee | 2023-12-14 20:26:26 +0100 | [diff] [blame] | 498 | var base: number |
| 499 | var height: number |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 500 | |
| 501 | def new(this.base, this.height) |
| 502 | enddef |
| 503 | endclass |
| 504 | < |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 505 | An abstract class is defined the same way as a normal class, except that it |
| 506 | does not have any new() method. *E1359* |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 507 | |
h_east | 596a9f2 | 2023-11-21 21:24:23 +0900 | [diff] [blame] | 508 | *abstract-method* *E1371* *E1372* |
Yegappan Lakshmanan | 7bcd25c | 2023-09-08 19:27:51 +0200 | [diff] [blame] | 509 | An abstract method can be defined in an abstract class by using the "abstract" |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 510 | prefix when defining the method: > |
Yegappan Lakshmanan | 7bcd25c | 2023-09-08 19:27:51 +0200 | [diff] [blame] | 511 | |
| 512 | abstract class Shape |
| 513 | abstract def Draw() |
| 514 | endclass |
Yegappan Lakshmanan | 00cd182 | 2023-09-18 19:56:49 +0200 | [diff] [blame] | 515 | < |
Yegappan Lakshmanan | ef9e3f8 | 2023-11-02 20:43:57 +0100 | [diff] [blame] | 516 | A static method in an abstract class cannot be an abstract method. |
| 517 | |
Yegappan Lakshmanan | 00cd182 | 2023-09-18 19:56:49 +0200 | [diff] [blame] | 518 | *E1373* |
Aliaksei Budavei | b214995 | 2024-01-07 17:52:10 +0300 | [diff] [blame] | 519 | A non-abstract class extending the abstract class must implement all the |
| 520 | abstract methods. The signature (arguments, argument types and return type) |
| 521 | must be exactly the same. If the return type of a method is a class, then |
| 522 | that class or one of its subclasses can be used in the extended method. |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 523 | |
| 524 | ============================================================================== |
| 525 | |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 526 | 5. Using an interface *Vim9-using-interface* |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 527 | |
| 528 | The example above with Shape, Square and Triangle can be made more useful if |
| 529 | we add a method to compute the surface of the object. For that we create the |
| 530 | interface called HasSurface, which specifies one method Surface() that returns |
| 531 | a number. This example extends the one above: > |
| 532 | |
| 533 | abstract class Shape |
Doug Kearns | 74da0ee | 2023-12-14 20:26:26 +0100 | [diff] [blame] | 534 | var color = Color.Black |
| 535 | var thickness = 10 |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 536 | endclass |
| 537 | |
| 538 | interface HasSurface |
| 539 | def Surface(): number |
| 540 | endinterface |
| 541 | |
| 542 | class Square extends Shape implements HasSurface |
Doug Kearns | 74da0ee | 2023-12-14 20:26:26 +0100 | [diff] [blame] | 543 | var size: number |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 544 | |
| 545 | def new(this.size) |
| 546 | enddef |
| 547 | |
| 548 | def Surface(): number |
| 549 | return this.size * this.size |
| 550 | enddef |
| 551 | endclass |
| 552 | |
| 553 | class Triangle extends Shape implements HasSurface |
Doug Kearns | 74da0ee | 2023-12-14 20:26:26 +0100 | [diff] [blame] | 554 | var base: number |
| 555 | var height: number |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 556 | |
| 557 | def new(this.base, this.height) |
| 558 | enddef |
| 559 | |
| 560 | def Surface(): number |
| 561 | return this.base * this.height / 2 |
| 562 | enddef |
| 563 | endclass |
Yegappan Lakshmanan | 00cd182 | 2023-09-18 19:56:49 +0200 | [diff] [blame] | 564 | < |
| 565 | *E1348* *E1349* *E1367* *E1382* *E1383* |
Bram Moolenaar | 1b5f03e | 2023-01-09 20:12:45 +0000 | [diff] [blame] | 566 | 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] | 567 | interface must appear in the class, with the same types. |
Bram Moolenaar | 1b5f03e | 2023-01-09 20:12:45 +0000 | [diff] [blame] | 568 | |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 569 | The interface name can be used as a type: > |
| 570 | |
| 571 | var shapes: list<HasSurface> = [ |
| 572 | Square.new(12), |
| 573 | Triangle.new(8, 15), |
| 574 | ] |
| 575 | for shape in shapes |
| 576 | echo $'the surface is {shape.Surface()}' |
| 577 | endfor |
Yegappan Lakshmanan | 00cd182 | 2023-09-18 19:56:49 +0200 | [diff] [blame] | 578 | < |
Yegappan Lakshmanan | 2dede3d | 2023-09-27 19:02:01 +0200 | [diff] [blame] | 579 | *E1378* *E1379* *E1380* *E1387* |
| 580 | An interface can contain only object methods and read-only object variables. |
Ernie Rael | 03042a2 | 2023-11-11 08:53:32 +0100 | [diff] [blame] | 581 | An interface cannot contain read-write or protected object variables, |
| 582 | protected object methods, class variables and class methods. |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 583 | |
Yegappan Lakshmanan | 00cd182 | 2023-09-18 19:56:49 +0200 | [diff] [blame] | 584 | An interface can extend another interface using "extends". The sub-interface |
| 585 | inherits all the instance variables and methods from the super interface. |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 586 | |
| 587 | ============================================================================== |
| 588 | |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 589 | 6. More class details *Vim9-class* *Class* *class* |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 590 | |
| 591 | Defining a class ~ |
| 592 | *:class* *:endclass* *:abstract* |
| 593 | A class is defined between `:class` and `:endclass`. The whole class is |
| 594 | defined in one script file. It is not possible to add to a class later. |
| 595 | |
Bram Moolenaar | 7db29e4 | 2022-12-11 15:53:04 +0000 | [diff] [blame] | 596 | A class can only be defined in a |Vim9| script file. *E1316* |
Yegappan Lakshmanan | c51578f | 2024-04-13 17:58:09 +0200 | [diff] [blame] | 597 | A class cannot be defined inside a function. *E1429* |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 598 | |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 599 | It is possible to define more than one class in a script file. Although it |
| 600 | usually is better to export only one main class. It can be useful to define |
| 601 | types, enums and helper classes though. |
| 602 | |
| 603 | The `:abstract` keyword may be prefixed and `:export` may be used. That gives |
| 604 | these variants: > |
| 605 | |
| 606 | class ClassName |
| 607 | endclass |
| 608 | |
| 609 | export class ClassName |
| 610 | endclass |
| 611 | |
| 612 | abstract class ClassName |
| 613 | endclass |
| 614 | |
| 615 | export abstract class ClassName |
| 616 | endclass |
| 617 | < |
| 618 | *E1314* |
| 619 | The class name should be CamelCased. It must start with an uppercase letter. |
| 620 | That avoids clashing with builtin types. |
Bram Moolenaar | 7db29e4 | 2022-12-11 15:53:04 +0000 | [diff] [blame] | 621 | *E1315* |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 622 | After the class name these optional items can be used. Each can appear only |
| 623 | once. They can appear in any order, although this order is recommended: > |
| 624 | extends ClassName |
| 625 | implements InterfaceName, OtherInterface |
| 626 | specifies SomeInterface |
Yegappan Lakshmanan | 44831e4 | 2025-02-16 16:15:50 +0100 | [diff] [blame] | 627 | < |
| 628 | The "specifies" feature is currently not implemented. |
| 629 | |
| 630 | *E1355* *E1369* |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 631 | Each variable and method name can be used only once. It is not possible to |
| 632 | define a method with the same name and different type of arguments. It is not |
Aliaksei Budavei | b214995 | 2024-01-07 17:52:10 +0300 | [diff] [blame] | 633 | possible to use a public and protected member variable with the same name. An |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 634 | object variable name used in a super class cannot be reused in a child class. |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 635 | |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 636 | Object Variable Initialization ~ |
Ernie Rael | 03042a2 | 2023-11-11 08:53:32 +0100 | [diff] [blame] | 637 | |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 638 | If the type of a variable is not explicitly specified in a class, then it is |
| 639 | set to "any" during class definition. When an object is instantiated from the |
| 640 | class, then the type of the variable is set. |
Yegappan Lakshmanan | 618e47d | 2023-08-22 21:29:28 +0200 | [diff] [blame] | 641 | |
Yegappan Lakshmanan | f3b68d4 | 2023-09-29 22:50:02 +0200 | [diff] [blame] | 642 | The following reserved keyword names cannot be used as an object or class |
| 643 | variable name: "super", "this", "true", "false", "null", "null_blob", |
Yegappan Lakshmanan | 8daae6f | 2025-04-05 16:00:22 +0200 | [diff] [blame] | 644 | "null_channel", "null_class", "null_dict", "null_function", "null_job", |
| 645 | "null_list", "null_object", "null_partial" and "null_string". |
Yegappan Lakshmanan | 618e47d | 2023-08-22 21:29:28 +0200 | [diff] [blame] | 646 | |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 647 | Extending a class ~ |
| 648 | *extends* |
Bram Moolenaar | 1b5f03e | 2023-01-09 20:12:45 +0000 | [diff] [blame] | 649 | A class can extend one other class. *E1352* *E1353* *E1354* |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 650 | The basic idea is to build on top of an existing class, add properties to it. |
| 651 | |
| 652 | The extended class is called the "base class" or "super class". The new class |
| 653 | is called the "child class". |
| 654 | |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 655 | Object variables from the base class are all taken over by the child class. It |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 656 | is not possible to override them (unlike some other languages). |
| 657 | |
| 658 | *E1356* *E1357* *E1358* |
Yegappan Lakshmanan | b32064f | 2023-10-02 21:43:58 +0200 | [diff] [blame] | 659 | Object methods of the base class can be overruled. The signature (arguments, |
Yegappan Lakshmanan | 26e8f7b | 2023-10-06 10:24:10 -0700 | [diff] [blame] | 660 | argument types and return type) must be exactly the same. If the return type |
| 661 | of a method is a class, then that class or one of its subclasses can be used |
| 662 | in the extended method. The method of the base class can be called by |
| 663 | prefixing "super.". |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 664 | |
Yegappan Lakshmanan | 00cd182 | 2023-09-18 19:56:49 +0200 | [diff] [blame] | 665 | *E1377* |
Ernie Rael | 03042a2 | 2023-11-11 08:53:32 +0100 | [diff] [blame] | 666 | The access level of a method (public or protected) in a child class should be |
Yegappan Lakshmanan | 00cd182 | 2023-09-18 19:56:49 +0200 | [diff] [blame] | 667 | the same as the super class. |
| 668 | |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 669 | Other object methods of the base class are taken over by the child class. |
| 670 | |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 671 | Class methods, including methods starting with "new", can be overruled, like |
| 672 | with object methods. The method on the base class can be called by prefixing |
| 673 | the name of the class (for class methods) or "super.". |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 674 | |
| 675 | Unlike other languages, the constructor of the base class does not need to be |
| 676 | invoked. In fact, it cannot be invoked. If some initialization from the base |
| 677 | class also needs to be done in a child class, put it in an object method and |
| 678 | call that method from every constructor(). |
| 679 | |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 680 | If the base class did not specify a new() method then one was automatically |
| 681 | created. This method will not be taken over by the child class. The child |
| 682 | class can define its own new() method, or, if there isn't one, a new() method |
| 683 | will be added automatically. |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 684 | |
| 685 | |
| 686 | A class implementing an interface ~ |
Yegappan Lakshmanan | 2dede3d | 2023-09-27 19:02:01 +0200 | [diff] [blame] | 687 | *implements* *E1346* *E1347* *E1389* |
Bram Moolenaar | 1b5f03e | 2023-01-09 20:12:45 +0000 | [diff] [blame] | 688 | A class can implement one or more interfaces. The "implements" keyword can |
| 689 | only appear once *E1350* . Multiple interfaces can be specified, separated by |
| 690 | commas. Each interface name can appear only once. *E1351* |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 691 | |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 692 | A class defining an interface ~ |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 693 | *specifies* |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 694 | A class can declare its interface, the object variables and methods, with a |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 695 | named interface. This avoids the need for separately specifying the |
Bram Moolenaar | 00b28d6 | 2022-12-08 15:32:33 +0000 | [diff] [blame] | 696 | interface, which is often done in many languages, especially Java. |
Yegappan Lakshmanan | 44831e4 | 2025-02-16 16:15:50 +0100 | [diff] [blame] | 697 | TODO: This is currently not implemented. |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 698 | |
Bram Moolenaar | 7db29e4 | 2022-12-11 15:53:04 +0000 | [diff] [blame] | 699 | Items in a class ~ |
Yegappan Lakshmanan | 2dede3d | 2023-09-27 19:02:01 +0200 | [diff] [blame] | 700 | *E1318* *E1325* *E1388* |
Bram Moolenaar | dd60c36 | 2023-02-27 15:49:53 +0000 | [diff] [blame] | 701 | Inside a class, in between `:class` and `:endclass`, these items can appear: |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 702 | - An object variable declaration: > |
Doug Kearns | 74da0ee | 2023-12-14 20:26:26 +0100 | [diff] [blame] | 703 | var _protectedVariableName: memberType |
| 704 | var readonlyVariableName: memberType |
| 705 | public var readwriteVariableName: memberType |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 706 | - A class variable declaration: > |
Doug Kearns | 74da0ee | 2023-12-14 20:26:26 +0100 | [diff] [blame] | 707 | static var _protectedClassVariableName: memberType |
| 708 | static var readonlyClassVariableName: memberType |
Aliaksei Budavei | b214995 | 2024-01-07 17:52:10 +0300 | [diff] [blame] | 709 | public static var readwriteClassVariableName: memberType |
Bram Moolenaar | 7db29e4 | 2022-12-11 15:53:04 +0000 | [diff] [blame] | 710 | - A constructor method: > |
Bram Moolenaar | 938ae28 | 2023-02-20 20:44:55 +0000 | [diff] [blame] | 711 | def new(arguments) |
| 712 | def newName(arguments) |
Yegappan Lakshmanan | 00cd182 | 2023-09-18 19:56:49 +0200 | [diff] [blame] | 713 | - A class method: > |
| 714 | static def SomeMethod(arguments) |
Ernie Rael | 03042a2 | 2023-11-11 08:53:32 +0100 | [diff] [blame] | 715 | static def _ProtectedMethod(arguments) |
Bram Moolenaar | 7db29e4 | 2022-12-11 15:53:04 +0000 | [diff] [blame] | 716 | - An object method: > |
Bram Moolenaar | 938ae28 | 2023-02-20 20:44:55 +0000 | [diff] [blame] | 717 | def SomeMethod(arguments) |
Ernie Rael | 03042a2 | 2023-11-11 08:53:32 +0100 | [diff] [blame] | 718 | def _ProtectedMethod(arguments) |
Yegappan Lakshmanan | 00cd182 | 2023-09-18 19:56:49 +0200 | [diff] [blame] | 719 | |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 720 | For the object variable the type must be specified. The best way is to do |
| 721 | this explicitly with ": {type}". For simple types you can also use an |
| 722 | initializer, such as "= 123", and Vim will see that the type is a number. |
| 723 | Avoid doing this for more complex types and when the type will be incomplete. |
| 724 | For example: > |
Doug Kearns | 74da0ee | 2023-12-14 20:26:26 +0100 | [diff] [blame] | 725 | var nameList = [] |
Bram Moolenaar | f1dcd14 | 2022-12-31 15:30:45 +0000 | [diff] [blame] | 726 | This specifies a list, but the item type is unknown. Better use: > |
Doug Kearns | 74da0ee | 2023-12-14 20:26:26 +0100 | [diff] [blame] | 727 | var nameList: list<string> |
Bram Moolenaar | f1dcd14 | 2022-12-31 15:30:45 +0000 | [diff] [blame] | 728 | The initialization isn't needed, the list is empty by default. |
| 729 | *E1330* |
| 730 | Some types cannot be used, such as "void", "null" and "v:none". |
Bram Moolenaar | 7db29e4 | 2022-12-11 15:53:04 +0000 | [diff] [blame] | 731 | |
Yegappan Lakshmanan | d3eae7b | 2024-03-03 16:26:58 +0100 | [diff] [blame] | 732 | Builtin Object Methods ~ |
| 733 | *builtin-object-methods* |
| 734 | Some of the builtin functions like |empty()|, |len()| and |string()| can be |
| 735 | used with an object. An object can implement a method with the same name as |
| 736 | these builtin functions to return an object-specific value. |
| 737 | |
Yegappan Lakshmanan | 44831e4 | 2025-02-16 16:15:50 +0100 | [diff] [blame] | 738 | *E1412* |
Yegappan Lakshmanan | d3eae7b | 2024-03-03 16:26:58 +0100 | [diff] [blame] | 739 | The following builtin methods are supported: |
Yegappan Lakshmanan | 44831e4 | 2025-02-16 16:15:50 +0100 | [diff] [blame] | 740 | *object-empty()* |
Yegappan Lakshmanan | d3eae7b | 2024-03-03 16:26:58 +0100 | [diff] [blame] | 741 | empty() Invoked by the |empty()| function to check whether an object is |
| 742 | empty. If this method is missing, then true is returned. This |
| 743 | method should not accept any arguments and must return a boolean. |
Yegappan Lakshmanan | 44831e4 | 2025-02-16 16:15:50 +0100 | [diff] [blame] | 744 | *object-len()* |
Yegappan Lakshmanan | d3eae7b | 2024-03-03 16:26:58 +0100 | [diff] [blame] | 745 | len() Invoked by the |len()| function to return the length of an |
| 746 | object. If this method is missing in the class, then an error is |
| 747 | given and zero is returned. This method should not accept any |
| 748 | arguments and must return a number. |
Yegappan Lakshmanan | 44831e4 | 2025-02-16 16:15:50 +0100 | [diff] [blame] | 749 | *object-string()* |
Yegappan Lakshmanan | d3eae7b | 2024-03-03 16:26:58 +0100 | [diff] [blame] | 750 | string() Invoked by the |string()| function to get a textual |
| 751 | representation of an object. Also used by the |:echo| command |
| 752 | for an object. If this method is missing in the class, then a |
| 753 | built-in default textual representation is used. This method |
| 754 | should not accept any arguments and must return a string. |
| 755 | |
Yegappan Lakshmanan | 44831e4 | 2025-02-16 16:15:50 +0100 | [diff] [blame] | 756 | *E1413* |
Yegappan Lakshmanan | d3eae7b | 2024-03-03 16:26:58 +0100 | [diff] [blame] | 757 | A class method cannot be used as a builtin method. |
Bram Moolenaar | 7db29e4 | 2022-12-11 15:53:04 +0000 | [diff] [blame] | 758 | |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 759 | Defining an interface ~ |
Christian Brabandt | 6c1afa3 | 2024-01-01 20:50:51 +0100 | [diff] [blame] | 760 | *Interface* *:interface* *:endinterface* |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 761 | An interface is defined between `:interface` and `:endinterface`. It may be |
| 762 | prefixed with `:export`: > |
| 763 | |
| 764 | interface InterfaceName |
| 765 | endinterface |
| 766 | |
| 767 | export interface InterfaceName |
| 768 | endinterface |
Bram Moolenaar | 1b5f03e | 2023-01-09 20:12:45 +0000 | [diff] [blame] | 769 | < *E1344* |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 770 | An interface can declare object variables, just like in a class but without |
| 771 | any initializer. |
Bram Moolenaar | 1b5f03e | 2023-01-09 20:12:45 +0000 | [diff] [blame] | 772 | *E1345* |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 773 | An interface can declare methods with `:def`, including the arguments and |
| 774 | return type, but without the body and without `:enddef`. Example: > |
| 775 | |
| 776 | interface HasSurface |
Doug Kearns | 74da0ee | 2023-12-14 20:26:26 +0100 | [diff] [blame] | 777 | var size: number |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 778 | def Surface(): number |
| 779 | endinterface |
| 780 | |
Bram Moolenaar | 1b5f03e | 2023-01-09 20:12:45 +0000 | [diff] [blame] | 781 | An interface name must start with an uppercase letter. *E1343* |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 782 | The "Has" prefix can be used to make it easier to guess this is an interface |
| 783 | name, with a hint about what it provides. |
Bram Moolenaar | 1b5f03e | 2023-01-09 20:12:45 +0000 | [diff] [blame] | 784 | An interface can only be defined in a |Vim9| script file. *E1342* |
Yegappan Lakshmanan | 00cd182 | 2023-09-18 19:56:49 +0200 | [diff] [blame] | 785 | An interface cannot "implement" another interface but it can "extend" another |
| 786 | interface. *E1381* |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 787 | |
Bram Moolenaar | 938ae28 | 2023-02-20 20:44:55 +0000 | [diff] [blame] | 788 | null object ~ |
| 789 | |
Bram Moolenaar | dd60c36 | 2023-02-27 15:49:53 +0000 | [diff] [blame] | 790 | 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] | 791 | initialized, the value is null. When trying to use this null object Vim often |
| 792 | does not know what class was supposed to be used. Vim then cannot check if |
Aliaksei Budavei | b214995 | 2024-01-07 17:52:10 +0300 | [diff] [blame] | 793 | a variable name is correct and you will get a "Using a null object" error, |
h_east | ba77bbb | 2023-10-03 04:47:13 +0900 | [diff] [blame] | 794 | even when the variable name is invalid. *E1360* *E1362* |
Bram Moolenaar | 938ae28 | 2023-02-20 20:44:55 +0000 | [diff] [blame] | 795 | |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 796 | Default constructor ~ |
Yegappan Lakshmanan | 413f839 | 2023-09-28 22:46:37 +0200 | [diff] [blame] | 797 | *default-constructor* |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 798 | In case you define a class without a new() method, one will be automatically |
| 799 | defined. This default constructor will have arguments for all the object |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 800 | variables, in the order they were specified. Thus if your class looks like: > |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 801 | |
| 802 | class AutoNew |
Doug Kearns | 74da0ee | 2023-12-14 20:26:26 +0100 | [diff] [blame] | 803 | var name: string |
| 804 | var age: number |
| 805 | var gender: Gender |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 806 | endclass |
| 807 | |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 808 | Then the default constructor will be: > |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 809 | |
Bram Moolenaar | 65b0d16 | 2022-12-13 18:43:22 +0000 | [diff] [blame] | 810 | 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] | 811 | enddef |
| 812 | |
Bram Moolenaar | 65b0d16 | 2022-12-13 18:43:22 +0000 | [diff] [blame] | 813 | The "= v:none" default values make the arguments optional. Thus you can also |
| 814 | call `new()` without any arguments. No assignment will happen and the default |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 815 | value for the object variables will be used. This is a more useful example, |
Bram Moolenaar | 65b0d16 | 2022-12-13 18:43:22 +0000 | [diff] [blame] | 816 | with default values: > |
Bram Moolenaar | 7db29e4 | 2022-12-11 15:53:04 +0000 | [diff] [blame] | 817 | |
| 818 | class TextPosition |
Doug Kearns | 74da0ee | 2023-12-14 20:26:26 +0100 | [diff] [blame] | 819 | var lnum: number = 1 |
| 820 | var col: number = 1 |
Bram Moolenaar | 7db29e4 | 2022-12-11 15:53:04 +0000 | [diff] [blame] | 821 | endclass |
| 822 | |
| 823 | If you want the constructor to have mandatory arguments, you need to write it |
| 824 | yourself. For example, if for the AutoNew class above you insist on getting |
| 825 | the name, you can define the constructor like this: > |
| 826 | |
Bram Moolenaar | 65b0d16 | 2022-12-13 18:43:22 +0000 | [diff] [blame] | 827 | def new(this.name, this.age = v:none, this.gender = v:none) |
Bram Moolenaar | 7db29e4 | 2022-12-11 15:53:04 +0000 | [diff] [blame] | 828 | enddef |
Yegappan Lakshmanan | 563e644 | 2023-12-05 08:19:06 -0800 | [diff] [blame] | 829 | < |
| 830 | When using the default new() method, if the order of the object variables in |
| 831 | the class is changed later, then all the callers of the default new() method |
Aliaksei Budavei | b214995 | 2024-01-07 17:52:10 +0300 | [diff] [blame] | 832 | need to change. To avoid this, the new() method can be explicitly defined |
Yegappan Lakshmanan | 563e644 | 2023-12-05 08:19:06 -0800 | [diff] [blame] | 833 | without any arguments. |
| 834 | |
| 835 | *E1328* |
Bram Moolenaar | 65b0d16 | 2022-12-13 18:43:22 +0000 | [diff] [blame] | 836 | Note that you cannot use another default value than "v:none" here. If you |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 837 | want to initialize the object variables, do it where they are declared. This |
Bram Moolenaar | 65b0d16 | 2022-12-13 18:43:22 +0000 | [diff] [blame] | 838 | 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] | 839 | |
Ernie Rael | 03042a2 | 2023-11-11 08:53:32 +0100 | [diff] [blame] | 840 | All object variables will be used in the default constructor, including |
| 841 | protected access ones. |
Bram Moolenaar | 1b5f03e | 2023-01-09 20:12:45 +0000 | [diff] [blame] | 842 | |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 843 | If the class extends another one, the object variables of that class will come |
Bram Moolenaar | 1b5f03e | 2023-01-09 20:12:45 +0000 | [diff] [blame] | 844 | first. |
| 845 | |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 846 | |
| 847 | Multiple constructors ~ |
errael | 92feeaf | 2023-12-27 10:07:09 -0800 | [diff] [blame] | 848 | *multiple-constructors* |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 849 | Normally a class has just one new() constructor. In case you find that the |
| 850 | constructor is often called with the same arguments you may want to simplify |
| 851 | your code by putting those arguments into a second constructor method. For |
| 852 | example, if you tend to use the color black a lot: > |
| 853 | |
| 854 | def new(this.garment, this.color, this.size) |
| 855 | enddef |
| 856 | ... |
| 857 | var pants = new(Garment.pants, Color.black, "XL") |
| 858 | var shirt = new(Garment.shirt, Color.black, "XL") |
| 859 | var shoes = new(Garment.shoes, Color.black, "45") |
| 860 | |
| 861 | Instead of repeating the color every time you can add a constructor that |
| 862 | includes it: > |
| 863 | |
| 864 | def newBlack(this.garment, this.size) |
| 865 | this.color = Color.black |
| 866 | enddef |
| 867 | ... |
| 868 | var pants = newBlack(Garment.pants, "XL") |
| 869 | var shirt = newBlack(Garment.shirt, "XL") |
| 870 | var shoes = newBlack(Garment.shoes, "9.5") |
| 871 | |
| 872 | Note that the method name must start with "new". If there is no method called |
| 873 | "new()" then the default constructor is added, even though there are other |
| 874 | constructor methods. |
| 875 | |
Yegappan Lakshmanan | 56d45f1 | 2024-11-11 19:58:55 +0100 | [diff] [blame] | 876 | Using variable type "any" for an Object~ |
| 877 | *obj-var-type-any* |
| 878 | You can use a variable declared with type "any" to hold an object. e.g. |
| 879 | > |
| 880 | vim9script |
| 881 | class A |
| 882 | var n = 10 |
| 883 | def Get(): number |
| 884 | return this.n |
| 885 | enddef |
| 886 | endclass |
| 887 | |
| 888 | def Fn(o: any) |
| 889 | echo o.n |
| 890 | echo o.Get() |
| 891 | enddef |
| 892 | |
| 893 | var a = A.new() |
| 894 | Fn(a) |
| 895 | < |
| 896 | In this example, Vim cannot determine the type of the parameter "o" for |
| 897 | function Fn() at compile time. It can be either a |Dict| or an |Object| |
| 898 | value. Therefore, at runtime, when the type is known, the object member |
| 899 | variable and method are looked up. This process is not efficient, so it is |
| 900 | recommended to use a more specific type whenever possible for better |
| 901 | efficiency. |
| 902 | |
Yegappan Lakshmanan | 4f32c83 | 2024-01-12 17:36:40 +0100 | [diff] [blame] | 903 | Compiling methods in a Class ~ |
| 904 | *class-compile* |
| 905 | The |:defcompile| command can be used to compile all the class and object |
| 906 | methods defined in a class: > |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 907 | |
Yegappan Lakshmanan | 4f32c83 | 2024-01-12 17:36:40 +0100 | [diff] [blame] | 908 | defcompile MyClass # Compile class "MyClass" |
| 909 | defcompile # Compile the classes in the current script |
| 910 | < |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 911 | ============================================================================== |
| 912 | |
Christian Brabandt | 6c1afa3 | 2024-01-01 20:50:51 +0100 | [diff] [blame] | 913 | 7. Type definition *typealias* *Vim9-type* *:type* |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 914 | |
Yegappan Lakshmanan | 2a71b54 | 2023-12-14 20:03:03 +0100 | [diff] [blame] | 915 | *E1393* *E1395* *E1396* *E1397* *E1398* |
| 916 | A type definition is giving a name to a type specification. This is also |
| 917 | known as a "type alias". The type alias can be used wherever a built-in type |
| 918 | can be used. Example: > |
Yegappan Lakshmanan | 26e8f7b | 2023-10-06 10:24:10 -0700 | [diff] [blame] | 919 | |
Yegappan Lakshmanan | 2a71b54 | 2023-12-14 20:03:03 +0100 | [diff] [blame] | 920 | type ListOfStrings = list<string> |
| 921 | var s: ListOfStrings = ['a', 'b'] |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 922 | |
Yegappan Lakshmanan | 2a71b54 | 2023-12-14 20:03:03 +0100 | [diff] [blame] | 923 | def ProcessStr(str: ListOfStrings): ListOfStrings |
| 924 | return str |
| 925 | enddef |
| 926 | echo ProcessStr(s) |
| 927 | < |
| 928 | *E1394* |
| 929 | A type alias name must start with an upper case character. Only existing |
| 930 | types can be aliased. |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 931 | |
Yegappan Lakshmanan | 2a71b54 | 2023-12-14 20:03:03 +0100 | [diff] [blame] | 932 | *E1399* |
| 933 | A type alias can be created only at the script level and not inside a |
| 934 | function. A type alias can be exported and used across scripts. |
| 935 | |
| 936 | *E1400* *E1401* *E1402* *E1403* *E1407* |
| 937 | A type alias cannot be used as an expression. A type alias cannot be used in |
| 938 | the left-hand-side of an assignment. |
| 939 | |
| 940 | For a type alias name, the |typename()| function returns the type that is |
| 941 | aliased: > |
| 942 | |
| 943 | type ListOfStudents = list<dict<any>> |
| 944 | echo typename(ListOfStudents) |
| 945 | typealias<list<dict<any>>> |
| 946 | < |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 947 | ============================================================================== |
| 948 | |
Bram Moolenaar | be4e016 | 2023-02-02 13:59:48 +0000 | [diff] [blame] | 949 | 8. Enum *Vim9-enum* *:enum* *:endenum* |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 950 | |
Yegappan Lakshmanan | 3164cf8 | 2024-03-28 10:36:42 +0100 | [diff] [blame] | 951 | *enum* *E1418* *E1419* *E1420* |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 952 | An enum is a type that can have one of a list of values. Example: > |
| 953 | |
Doug Kearns | 49a35f6 | 2024-12-29 15:33:12 +0100 | [diff] [blame] | 954 | enum Color |
Yegappan Lakshmanan | 3164cf8 | 2024-03-28 10:36:42 +0100 | [diff] [blame] | 955 | White, |
| 956 | Red, |
| 957 | Green, Blue, Black |
Doug Kearns | 49a35f6 | 2024-12-29 15:33:12 +0100 | [diff] [blame] | 958 | endenum |
Yegappan Lakshmanan | 3164cf8 | 2024-03-28 10:36:42 +0100 | [diff] [blame] | 959 | < |
| 960 | *enumvalue* *E1422* |
| 961 | The enum values are separated by commas. More than one enum value can be |
| 962 | listed in a single line. The final enum value should not be followed by a |
| 963 | comma. |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 964 | |
Yegappan Lakshmanan | 3164cf8 | 2024-03-28 10:36:42 +0100 | [diff] [blame] | 965 | An enum value is accessed using the enum name followed by the value name: > |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 966 | |
Yegappan Lakshmanan | 3164cf8 | 2024-03-28 10:36:42 +0100 | [diff] [blame] | 967 | var a: Color = Color.Blue |
| 968 | < |
| 969 | Enums are treated as classes, where each enum value is essentially an instance |
| 970 | of that class. Unlike typical object instantiation with the |new()| method, |
| 971 | enum instances cannot be created this way. |
| 972 | |
| 973 | An enum can only be defined in a |Vim9| script file. *E1414* |
| 974 | An enum cannot be defined inside a function. |
| 975 | |
| 976 | *E1415* |
| 977 | An enum name must start with an uppercase letter. The name of an enum value |
| 978 | in an enum can start with an upper or lowercase letter. |
| 979 | |
| 980 | *E1416* |
| 981 | An enum can implement an interface but cannot extend a class: > |
| 982 | |
| 983 | enum MyEnum implements MyIntf |
| 984 | Value1, |
| 985 | Value2 |
| 986 | |
| 987 | def SomeMethod() |
| 988 | enddef |
| 989 | endenum |
| 990 | < |
| 991 | *enum-constructor* |
| 992 | The enum value objects in an enum are constructed like any other objects using |
| 993 | the |new()| method. Arguments can be passed to the enum constructor by |
| 994 | specifying them after the enum value name, just like calling a function. The |
| 995 | default constructor doesn't have any arguments. |
| 996 | |
| 997 | *E1417* |
| 998 | An enum can contain class variables, class methods, object variables and |
| 999 | object methods. The methods in an enum cannot be |:abstract| methods. |
| 1000 | |
| 1001 | The following example shows an enum with object variables and methods: > |
| 1002 | |
| 1003 | vim9script |
| 1004 | enum Planet |
| 1005 | Earth(1, false), |
| 1006 | Jupiter(95, true), |
| 1007 | Saturn(146, true) |
| 1008 | |
| 1009 | var moons: number |
| 1010 | var has_rings: bool |
| 1011 | def GetMoons(): number |
| 1012 | return this.moons |
| 1013 | enddef |
| 1014 | endenum |
| 1015 | echo Planet.Jupiter.GetMoons() |
| 1016 | echo Planet.Earth.has_rings |
| 1017 | < |
| 1018 | *E1421* *E1423* *E1424* *E1425* |
Aliaksei Budavei | 9574022 | 2024-04-04 23:05:33 +0300 | [diff] [blame] | 1019 | Enums and their values are immutable. They cannot be utilized as numerical or |
| 1020 | string types. Enum values can declare mutable instance variables. |
Yegappan Lakshmanan | 3164cf8 | 2024-03-28 10:36:42 +0100 | [diff] [blame] | 1021 | |
| 1022 | *enum-name* |
| 1023 | Each enum value object has a "name" instance variable which contains the name |
| 1024 | of the enum value. This is a readonly variable. |
| 1025 | |
| 1026 | *enum-ordinal* *E1426* |
| 1027 | Each enum value has an associated ordinal number starting with 0. The ordinal |
| 1028 | number of an enum value can be accessed using the "ordinal" instance variable. |
| 1029 | This is a readonly variable. Note that if the ordering of the enum values in |
| 1030 | an enum is changed, then their ordinal values will also change. |
| 1031 | |
| 1032 | *enum-values* |
| 1033 | All the values in an enum can be accessed using the "values" class variable |
| 1034 | which is a List of the enum objects. This is a readonly variable. |
| 1035 | |
| 1036 | Example: > |
| 1037 | enum Planet |
| 1038 | Mercury, |
| 1039 | Venus, |
| 1040 | Earth |
| 1041 | endenum |
| 1042 | |
| 1043 | echo Planet.Mercury |
| 1044 | echo Planet.Venus.name |
| 1045 | echo Planet.Venus.ordinal |
| 1046 | for p in Planet.values |
| 1047 | # ... |
| 1048 | endfor |
| 1049 | < |
| 1050 | An enum is a class with class variables for the enum value objects and object |
| 1051 | variables for the enum value name and the enum value ordinal: > |
| 1052 | |
| 1053 | enum Planet |
| 1054 | Mercury, |
| 1055 | Venus |
| 1056 | endenum |
| 1057 | < |
| 1058 | The above enum definition is equivalent to the following class definition: > |
| 1059 | |
| 1060 | class Planet |
| 1061 | public static final Mercury: Planet = Planet.new('Mercury', 0) |
| 1062 | public static final Venus: Planet = Planet.new('Venus', 1) |
| 1063 | |
| 1064 | public static const values: list<Planet> = [Planet.Mercury, Planet.Venus] |
| 1065 | |
| 1066 | public const name: string |
| 1067 | public const ordinal: number |
| 1068 | endclass |
| 1069 | < |
Hirohito Higashi | 31b78cc | 2025-04-21 19:39:15 +0200 | [diff] [blame] | 1070 | A enum can contain object variables and methods just like a regular class: > |
Yegappan Lakshmanan | 4ec93fe | 2025-04-14 21:14:33 +0200 | [diff] [blame] | 1071 | |
| 1072 | enum Color |
| 1073 | Cyan([0, 255, 255]), |
| 1074 | Magenta([255, 0, 255]), |
| 1075 | Gray([128, 128, 128]) |
| 1076 | |
| 1077 | var rgb_values: list<number> |
| 1078 | |
| 1079 | def Get_RGB(): list<number> |
| 1080 | return this.rgb_values |
| 1081 | enddef |
| 1082 | endenum |
| 1083 | echo Color.Magenta.Get_RGB() |
| 1084 | < |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 1085 | ============================================================================== |
| 1086 | |
| 1087 | 9. Rationale |
| 1088 | |
| 1089 | Most of the choices for |Vim9| classes come from popular and recently |
| 1090 | developed languages, such as Java, TypeScript and Dart. The syntax has been |
| 1091 | made to fit with the way Vim script works, such as using `endclass` instead of |
| 1092 | using curly braces around the whole class. |
| 1093 | |
| 1094 | Some common constructs of object-oriented languages were chosen very long ago |
| 1095 | when this kind of programming was still new, and later found to be |
| 1096 | sub-optimal. By this time those constructs were widely used and changing them |
| 1097 | was not an option. In Vim we do have the freedom to make different choices, |
| 1098 | since classes are completely new. We can make the syntax simpler and more |
| 1099 | consistent than what "old" languages use. Without diverting too much, it |
| 1100 | should still mostly look like what you know from existing languages. |
| 1101 | |
| 1102 | Some recently developed languages add all kinds of fancy features that we |
| 1103 | don't need for Vim. But some have nice ideas that we do want to use. |
| 1104 | Thus we end up with a base of what is common in popular languages, dropping |
| 1105 | what looks like a bad idea, and adding some nice features that are easy to |
| 1106 | understand. |
| 1107 | |
| 1108 | The main rules we use to make decisions: |
| 1109 | - Keep it simple. |
| 1110 | - No surprises, mostly do what other languages are doing. |
| 1111 | - Avoid mistakes from the past. |
| 1112 | - Avoid the need for the script writer to consult the help to understand how |
| 1113 | things work, most things should be obvious. |
| 1114 | - Keep it consistent. |
| 1115 | - Aim at an average size plugin, not at a huge project. |
| 1116 | |
| 1117 | |
| 1118 | Using new() for the constructor ~ |
| 1119 | |
| 1120 | Many languages use the class name for the constructor method. A disadvantage |
| 1121 | is that quite often this is a long name. And when changing the class name all |
| 1122 | constructor methods need to be renamed. Not a big deal, but still a |
| 1123 | disadvantage. |
| 1124 | |
| 1125 | Other languages, such as TypeScript, use a specific name, such as |
| 1126 | "constructor()". That seems better. However, using "new" or "new()" to |
| 1127 | create a new object has no obvious relation with "constructor()". |
| 1128 | |
| 1129 | For |Vim9| script using the same method name for all constructors seemed like |
| 1130 | the right choice, and by calling it new() the relation between the caller and |
| 1131 | the method being called is obvious. |
| 1132 | |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 1133 | No overloading of the constructor ~ |
| 1134 | |
| 1135 | In Vim script, both legacy and |Vim9| script, there is no overloading of |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 1136 | methods. That means it is not possible to use the same method name with |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 1137 | different types of arguments. Therefore there also is only one new() |
| 1138 | constructor. |
| 1139 | |
| 1140 | With |Vim9| script it would be possible to support overloading, since |
| 1141 | arguments are typed. However, this gets complicated very quickly. Looking at |
| 1142 | a new() call one has to inspect the types of the arguments to know which of |
| 1143 | several new() methods is actually being called. And that can require |
| 1144 | inspecting quite a bit of code. For example, if one of the arguments is the |
| 1145 | return value of a method, you need to find that method to see what type it is |
| 1146 | returning. |
| 1147 | |
| 1148 | Instead, every constructor has to have a different name, starting with "new". |
| 1149 | That way multiple constructors with different arguments are possible, while it |
| 1150 | is very easy to see which constructor is being used. And the type of |
| 1151 | arguments can be properly checked. |
| 1152 | |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 1153 | No overloading of methods ~ |
| 1154 | |
| 1155 | Same reasoning as for the constructor: It is often not obvious what type |
| 1156 | arguments have, which would make it difficult to figure out what method is |
| 1157 | actually being called. Better just give the methods a different name, then |
| 1158 | type checking will make sure it works as you intended. This rules out |
| 1159 | polymorphism, which we don't really need anyway. |
| 1160 | |
Bram Moolenaar | 1b5f03e | 2023-01-09 20:12:45 +0000 | [diff] [blame] | 1161 | Single inheritance and interfaces ~ |
| 1162 | |
| 1163 | Some languages support multiple inheritance. Although that can be useful in |
| 1164 | some cases, it makes the rules of how a class works quite complicated. |
| 1165 | Instead, using interfaces to declare what is supported is much simpler. The |
| 1166 | 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] | 1167 | Vim. The "keep it simple" rule applies here. |
Bram Moolenaar | 1b5f03e | 2023-01-09 20:12:45 +0000 | [diff] [blame] | 1168 | |
| 1169 | Explicitly declaring that a class supports an interface makes it easy to see |
| 1170 | what a class is intended for. It also makes it possible to do proper type |
| 1171 | checking. When an interface is changed any class that declares to implement |
| 1172 | it will be checked if that change was also changed. The mechanism to assume a |
| 1173 | class implements an interface just because the methods happen to match is |
| 1174 | brittle and leads to obscure problems, let's not do that. |
| 1175 | |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 1176 | Using "this.variable" everywhere ~ |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 1177 | |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 1178 | The object variables in various programming languages can often be accessed in |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 1179 | different ways, depending on the location. Sometimes "this." has to be |
| 1180 | prepended to avoid ambiguity. They are usually declared without "this.". |
| 1181 | That is quite inconsistent and sometimes confusing. |
| 1182 | |
| 1183 | A very common issue is that in the constructor the arguments use the same name |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 1184 | as the object variable. Then for these variables "this." needs to be prefixed |
| 1185 | in the body, while for other variables this is not needed and often omitted. |
| 1186 | This leads to a mix of variables with and without "this.", which is |
| 1187 | inconsistent. |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 1188 | |
Aliaksei Budavei | b214995 | 2024-01-07 17:52:10 +0300 | [diff] [blame] | 1189 | For |Vim9| classes the "this." prefix is always used for declared methods and |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 1190 | variables. Simple and consistent. When looking at the code inside a class |
| 1191 | it's also directly clear which variable references are object variables and |
| 1192 | which aren't. |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 1193 | |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 1194 | Using class variables ~ |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 1195 | |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 1196 | Using "static variable" to declare a class variable is very common, nothing |
| 1197 | new here. In |Vim9| script these can be accessed directly by their name. |
| 1198 | Very much like how a script-local variable can be used in a method. Since |
| 1199 | object variables are always accessed with "this." prepended, it's also quickly |
| 1200 | clear what kind of variable it is. |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 1201 | |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 1202 | TypeScript prepends the class name before the class variable name, also inside |
| 1203 | the class. This has two problems: The class name can be rather long, taking |
| 1204 | up quite a bit of space, and when the class is renamed all these places need |
| 1205 | to be changed too. |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 1206 | |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 1207 | Declaring object and class variables ~ |
Bram Moolenaar | 1b5f03e | 2023-01-09 20:12:45 +0000 | [diff] [blame] | 1208 | |
| 1209 | The main choice is whether to use "var" as with variable declarations. |
| 1210 | TypeScript does not use it: > |
| 1211 | class Point { |
| 1212 | x: number; |
| 1213 | y = 0; |
| 1214 | } |
| 1215 | |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 1216 | Following that Vim object variables could be declared like this: > |
Bram Moolenaar | 1b5f03e | 2023-01-09 20:12:45 +0000 | [diff] [blame] | 1217 | class Point |
| 1218 | this.x: number |
| 1219 | this.y = 0 |
| 1220 | endclass |
| 1221 | |
| 1222 | Some users pointed out that this looks more like an assignment than a |
Aliaksei Budavei | b214995 | 2024-01-07 17:52:10 +0300 | [diff] [blame] | 1223 | declaration. Adding "var" and omitting "this." changes that: > |
Bram Moolenaar | 1b5f03e | 2023-01-09 20:12:45 +0000 | [diff] [blame] | 1224 | class Point |
Doug Kearns | 74da0ee | 2023-12-14 20:26:26 +0100 | [diff] [blame] | 1225 | var x: number |
| 1226 | var y = 0 |
Bram Moolenaar | 1b5f03e | 2023-01-09 20:12:45 +0000 | [diff] [blame] | 1227 | endclass |
| 1228 | |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 1229 | We also need to be able to declare class variables using the "static" keyword. |
Bram Moolenaar | 1b5f03e | 2023-01-09 20:12:45 +0000 | [diff] [blame] | 1230 | There we can also choose to leave out "var": > |
| 1231 | class Point |
Doug Kearns | 74da0ee | 2023-12-14 20:26:26 +0100 | [diff] [blame] | 1232 | var x: number |
Bram Moolenaar | 1b5f03e | 2023-01-09 20:12:45 +0000 | [diff] [blame] | 1233 | static count = 0 |
| 1234 | endclass |
| 1235 | |
| 1236 | Or do use it, before "static": > |
| 1237 | class Point |
Doug Kearns | 74da0ee | 2023-12-14 20:26:26 +0100 | [diff] [blame] | 1238 | var x: number |
Bram Moolenaar | 1b5f03e | 2023-01-09 20:12:45 +0000 | [diff] [blame] | 1239 | var static count = 0 |
| 1240 | endclass |
| 1241 | |
| 1242 | Or after "static": > |
| 1243 | class Point |
Doug Kearns | 74da0ee | 2023-12-14 20:26:26 +0100 | [diff] [blame] | 1244 | var x: number |
Bram Moolenaar | 1b5f03e | 2023-01-09 20:12:45 +0000 | [diff] [blame] | 1245 | static var count = 0 |
| 1246 | endclass |
| 1247 | |
| 1248 | This is more in line with "static def Func()". |
| 1249 | |
| 1250 | There is no clear preference whether to use "var" or not. The two main |
| 1251 | reasons to leave it out are: |
Doug Kearns | 74da0ee | 2023-12-14 20:26:26 +0100 | [diff] [blame] | 1252 | 1. TypeScript and other popular languages do not use it. |
Bram Moolenaar | 1b5f03e | 2023-01-09 20:12:45 +0000 | [diff] [blame] | 1253 | 2. Less clutter. |
| 1254 | |
Doug Kearns | 74da0ee | 2023-12-14 20:26:26 +0100 | [diff] [blame] | 1255 | However, it is more common for languages to reuse their general variable and |
| 1256 | function declaration syntax for class/object variables and methods. Vim9 also |
| 1257 | reuses the general function declaration syntax for methods. So, for the sake |
| 1258 | of consistency, we require "var" in these declarations. |
| 1259 | |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 1260 | Using "ClassName.new()" to construct an object ~ |
| 1261 | |
| 1262 | Many languages use the "new" operator to create an object, which is actually |
| 1263 | kind of strange, since the constructor is defined as a method with arguments, |
| 1264 | not a command. TypeScript also has the "new" keyword, but the method is |
| 1265 | called "constructor()", it is hard to see the relation between the two. |
| 1266 | |
| 1267 | In |Vim9| script the constructor method is called new(), and it is invoked as |
| 1268 | new(), simple and straightforward. Other languages use "new ClassName()", |
| 1269 | while there is no ClassName() method, it's a method by another name in the |
| 1270 | class called ClassName. Quite confusing. |
| 1271 | |
| 1272 | |
Ernie Rael | 03042a2 | 2023-11-11 08:53:32 +0100 | [diff] [blame] | 1273 | Vim9class access modes ~ |
| 1274 | *vim9-access-modes* |
| 1275 | The variable access modes, and their meaning, supported by Vim9class are |
| 1276 | |public-variable| read and write from anywhere |
| 1277 | |read-only-variable| read from anywhere, write from inside the |
| 1278 | class and sub-classes |
| 1279 | |protected-variable| read and write from inside the class and |
| 1280 | sub-classes |
| 1281 | |
| 1282 | The method access modes are similar, but without the read-only mode. |
| 1283 | |
| 1284 | |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 1285 | Default read access to object variables ~ |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 1286 | |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 1287 | Some users will remark that the access rules for object variables are |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 1288 | asymmetric. Well, that is intentional. Changing a value is a very different |
| 1289 | action than reading a value. The read operation has no side effects, it can |
| 1290 | be done any number of times without affecting the object. Changing the value |
| 1291 | can have many side effects, and even have a ripple effect, affecting other |
| 1292 | objects. |
| 1293 | |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 1294 | When adding object variables one usually doesn't think much about this, just |
| 1295 | get the type right. And normally the values are set in the new() method. |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 1296 | Therefore defaulting to read access only "just works" in most cases. And when |
| 1297 | directly writing you get an error, which makes you wonder if you actually want |
| 1298 | to allow that. This helps writing code with fewer mistakes. |
| 1299 | |
| 1300 | |
Ernie Rael | 03042a2 | 2023-11-11 08:53:32 +0100 | [diff] [blame] | 1301 | Making object variables protected with an underscore ~ |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 1302 | |
Ernie Rael | 03042a2 | 2023-11-11 08:53:32 +0100 | [diff] [blame] | 1303 | When an object variable is protected, it can only be read and changed inside |
| 1304 | the class (and in sub-classes), then it cannot be used outside of the class. |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 1305 | Prepending an underscore is a simple way to make that visible. Various |
| 1306 | programming languages have this as a recommendation. |
| 1307 | |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 1308 | In case you change your mind and want to make the object variable accessible |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 1309 | outside of the class, you will have to remove the underscore everywhere. |
| 1310 | Since the name only appears in the class (and sub-classes) they will be easy |
| 1311 | to find and change. |
| 1312 | |
| 1313 | The other way around is much harder: you can easily prepend an underscore to |
Ernie Rael | 03042a2 | 2023-11-11 08:53:32 +0100 | [diff] [blame] | 1314 | the object variable inside the class to make it protected, but any usage |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 1315 | elsewhere you will have to track down and change. You may have to make it a |
| 1316 | "set" method call. This reflects the real world problem that taking away |
| 1317 | access requires work to be done for all places where that access exists. |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 1318 | |
Ernie Rael | 03042a2 | 2023-11-11 08:53:32 +0100 | [diff] [blame] | 1319 | An alternative would have been using the "protected" keyword, just like |
| 1320 | "public" changes the access in the other direction. Well, that's just to |
| 1321 | reduce the number of keywords. |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 1322 | |
Ernie Rael | 03042a2 | 2023-11-11 08:53:32 +0100 | [diff] [blame] | 1323 | No private object variables ~ |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 1324 | |
Yegappan Lakshmanan | c3b315f | 2023-09-24 14:36:17 -0700 | [diff] [blame] | 1325 | Some languages provide several ways to control access to object variables. |
| 1326 | The most known is "protected", and the meaning varies from language to |
Ernie Rael | 03042a2 | 2023-11-11 08:53:32 +0100 | [diff] [blame] | 1327 | language. Others are "shared", "private", "package" and even "friend". |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 1328 | |
| 1329 | These rules make life more difficult. That can be justified in projects where |
| 1330 | many people work on the same, complex code where it is easy to make mistakes. |
| 1331 | Especially when refactoring or other changes to the class model. |
| 1332 | |
| 1333 | The Vim scripts are expected to be used in a plugin, with just one person or a |
| 1334 | 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] | 1335 | the extra safety provided by the rules isn't really needed. Let's just keep |
| 1336 | it simple and not specify access details. |
Bram Moolenaar | c1c365c | 2022-12-04 20:13:24 +0000 | [diff] [blame] | 1337 | |
| 1338 | |
| 1339 | ============================================================================== |
| 1340 | |
| 1341 | 10. To be done later |
| 1342 | |
| 1343 | Can a newSomething() constructor invoke another constructor? If yes, what are |
| 1344 | the restrictions? |
| 1345 | |
| 1346 | Thoughts: |
| 1347 | - Generics for a class: `class <Tkey, Tentry>` |
| 1348 | - Generics for a function: `def <Tkey> GetLast(key: Tkey)` |
| 1349 | - Mixins: not sure if that is useful, leave out for simplicity. |
| 1350 | |
| 1351 | Some things that look like good additions: |
| 1352 | - For testing: Mock mechanism |
| 1353 | |
| 1354 | An important class to be provided is "Promise". Since Vim is single |
| 1355 | threaded, connecting asynchronous operations is a natural way of allowing |
| 1356 | plugins to do their work without blocking the user. It's a uniform way to |
| 1357 | invoke callbacks and handle timeouts and errors. |
| 1358 | |
| 1359 | |
| 1360 | vim:tw=78:ts=8:noet:ft=help:norl: |