blob: 53b72fc062d8eac2c571401068d51e4501d855b6 [file] [log] [blame]
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02001*vim9class.txt* For Vim version 9.0. Last change: 2023 Sep 18
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00002
3
4 VIM REFERENCE MANUAL by Bram Moolenaar
5
6
Bram Moolenaarbe4e0162023-02-02 13:59:48 +00007NOTE - This is not finished yet, anything can still change! - NOTE
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00008
9
10Vim9 classes, objects, interfaces, types and enums.
11
121. Overview |Vim9-class-overview|
132. A simple class |Vim9-simple-class|
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200143. Class variables and methods |Vim9-class-member|
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000154. Using an abstract class |Vim9-abstract-class|
165. Using an interface |Vim9-using-interface|
176. More class details |Vim9-class|
187. Type definition |Vim9-type|
198. Enum |Vim9-enum|
Bram Moolenaarc1c365c2022-12-04 20:13:24 +000020
219. Rationale
2210. To be done later
23
24==============================================================================
25
261. Overview *Vim9-class-overview*
27
28The fancy term is "object-oriented programming". You can find lots of study
Bram Moolenaarbe4e0162023-02-02 13:59:48 +000029material on this subject. Here we document what |Vim9| script provides,
30assuming you know the basics already. Added are helpful hints about how to
Yegappan Lakshmanan0ab500d2023-10-21 11:59:42 +020031use this functionality effectively. Vim9 classes and objects cannot be used
32in legacy Vim scripts and legacy functions.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +000033
34The basic item is an object:
35- An object stores state. It contains one or more variables that can each
36 have a value.
Bram Moolenaarbe4e0162023-02-02 13:59:48 +000037- An object provides functions that use and manipulate its state. These
Bram Moolenaarc1c365c2022-12-04 20:13:24 +000038 functions are invoked "on the object", which is what sets it apart from the
39 traditional separation of data and code that manipulates the data.
40- An object has a well defined interface, with typed member variables and
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -070041 methods.
Bram Moolenaarbe4e0162023-02-02 13:59:48 +000042- Objects are created from a class and all objects have the same interface.
43 This does not change at runtime, it is not dynamic.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +000044
45An object can only be created by a class. A class provides:
46- A new() method, the constructor, which returns an object for the class.
47 This method is invoked on the class name: MyClass.new().
Bram Moolenaarbe4e0162023-02-02 13:59:48 +000048- State shared by all objects of the class: class variables (class members).
Bram Moolenaarc1c365c2022-12-04 20:13:24 +000049- A hierarchy of classes, with super-classes and sub-classes, inheritance.
50
51An interface is used to specify properties of an object:
52- An object can declare several interfaces that it implements.
53- Different objects implementing the same interface can be used the same way.
54
55The class hierarchy allows for single inheritance. Otherwise interfaces are
56to be used where needed.
57
58
59Class modeling ~
60
61You can model classes any way you like. Keep in mind what you are building,
62don't try to model the real world. This can be confusing, especially because
63teachers use real-world objects to explain class relations and you might think
64your model should therefore reflect the real world. It doesn't! The model
65should match your purpose.
66
Bram Moolenaarbe4e0162023-02-02 13:59:48 +000067Keep in mind that composition (an object contains other objects) is often
68better than inheritance (an object extends another object). Don't waste time
69trying to find the optimal class model. Or waste time discussing whether a
70square is a rectangle or that a rectangle is a square. It doesn't matter.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +000071
72
73==============================================================================
74
752. A simple class *Vim9-simple-class*
76
Bram Moolenaarbe4e0162023-02-02 13:59:48 +000077Let's start with a simple example: a class that stores a text position (see
78below for how to do this more efficiently): >
Bram Moolenaarc1c365c2022-12-04 20:13:24 +000079
80 class TextPosition
81 this.lnum: number
82 this.col: number
83
84 def new(lnum: number, col: number)
85 this.lnum = lnum
86 this.col = col
87 enddef
88
89 def SetLnum(lnum: number)
90 this.lnum = lnum
91 enddef
92
93 def SetCol(col: number)
94 this.col = col
95 enddef
96
97 def SetPosition(lnum: number, col: number)
98 this.lnum = lnum
99 this.col = col
100 enddef
101 endclass
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000102< *object* *Object*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000103You can create an object from this class with the new() method: >
104
105 var pos = TextPosition.new(1, 1)
106
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700107The object variables "lnum" and "col" can be accessed directly: >
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000108
109 echo $'The text position is ({pos.lnum}, {pos.col})'
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000110< *E1317* *E1327*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000111If you have been using other object-oriented languages you will notice that
112in Vim the object members are consistently referred to with the "this."
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000113prefix. This is different from languages like Java and TypeScript. The
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000114naming convention makes the object members easy to spot. Also, when a
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700115variable does not have the "this." prefix you know it is not an object
116variable.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000117
118
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700119Object variable write access ~
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000120
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700121Now try to change an object variable directly: >
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000122
123 pos.lnum = 9
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000124< *E1335*
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700125This will give you an error! That is because by default object variables can
126be read but not set. That's why the TextPosition class provides a method for
127it: >
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000128
129 pos.SetLnum(9)
130
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700131Allowing to read but not set an object variable is the most common and safest
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000132way. Most often there is no problem using a value, while setting a value may
133have side effects that need to be taken care of. In this case, the SetLnum()
134method could check if the line number is valid and either give an error or use
135the closest valid value.
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000136 *:public* *E1331*
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700137If you don't care about side effects and want to allow the object variable to
138be changed at any time, you can make it public: >
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000139
140 public this.lnum: number
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000141 public this.col: number
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000142
143Now you don't need the SetLnum(), SetCol() and SetPosition() methods, setting
144"pos.lnum" directly above will no longer give an error.
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200145 *E1326*
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700146If you try to set an object variable that doesn't exist you get an error: >
Bram Moolenaarf1dcd142022-12-31 15:30:45 +0000147 pos.other = 9
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200148< E1326: Member not found on object "TextPosition": other ~
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000149
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200150 *E1376*
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700151A object variable cannot be accessed using the class name.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000152
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700153Private variables ~
Yegappan Lakshmanan413f8392023-09-28 22:46:37 +0200154 *private-variable* *E1332* *E1333*
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700155On the other hand, if you do not want the object variables to be read directly,
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000156you can make them private. This is done by prefixing an underscore to the
157name: >
158
159 this._lnum: number
160 this._col number
161
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700162Now you need to provide methods to get the value of the private variables.
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000163These are commonly called getters. We recommend using a name that starts with
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000164"Get": >
165
166 def GetLnum(): number
167 return this._lnum
168 enddef
169
170 def GetCol() number
171 return this._col
172 enddef
173
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700174This example isn't very useful, the variables might as well have been public.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000175It does become useful if you check the value. For example, restrict the line
176number to the total number of lines: >
177
178 def GetLnum(): number
179 if this._lnum > this._lineCount
180 return this._lineCount
181 endif
182 return this._lnum
183 enddef
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200184<
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +0200185Private methods ~
Yegappan Lakshmanan413f8392023-09-28 22:46:37 +0200186 *private-method* *E1366*
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +0200187If you want object methods to be accessible only from other methods of the
188same class and not used from outside the class, then you can make them
189private. This is done by prefixing the method name with an underscore: >
190
191 class SomeClass
192 def _Foo(): number
193 return 10
194 enddef
195 def Bar(): number
196 return this._Foo()
197 enddef
198 endclass
199<
200Accessing a private method outside the class will result in an error (using
201the above class): >
202
203 var a = SomeClass.new()
204 a._Foo()
205<
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000206Simplifying the new() method ~
Yegappan Lakshmanan413f8392023-09-28 22:46:37 +0200207 *new()* *constructor*
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700208Many constructors take values for the object variables. Thus you very often
209see this pattern: >
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000210
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000211 class SomeClass
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000212 this.lnum: number
213 this.col: number
214
215 def new(lnum: number, col: number)
216 this.lnum = lnum
217 this.col = col
218 enddef
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000219 endclass
h-eastdb385522023-09-28 22:18:19 +0200220<
221 *E1390*
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700222Not only is this text you need to write, it also has the type of each
223variables twice. Since this is so common a shorter way to write new() is
224provided: >
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000225
226 def new(this.lnum, this.col)
227 enddef
228
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700229The semantics are easy to understand: Providing the object variable name,
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000230including "this.", as the argument to new() means the value provided in the
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700231new() call is assigned to that object variable. This mechanism comes from the
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000232Dart language.
233
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700234Putting together this way of using new() and making the variables public
235results in a much shorter class definition than what we started with: >
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000236
237 class TextPosition
238 public this.lnum: number
239 public this.col: number
240
241 def new(this.lnum, this.col)
242 enddef
243
244 def SetPosition(lnum: number, col: number)
245 this.lnum = lnum
246 this.col = col
247 enddef
248 endclass
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000249
250The sequence of constructing a new object is:
2511. Memory is allocated and cleared. All values are zero/false/empty.
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -07002522. For each declared object variable that has an initializer, the expression
253 is evaluated and assigned to the variable. This happens in the sequence
254 the variables are declared in the class.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00002553. Arguments in the new() method in the "this.name" form are assigned.
2564. The body of the new() method is executed.
257
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000258If the class extends a parent class, the same thing happens. In the second
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700259step the object variables of the parent class are initialized first. There is
260no need to call "super()" or "new()" on the parent.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000261
Yegappan Lakshmananb90e3bc2023-09-28 23:06:48 +0200262 *E1365*
Yegappan Lakshmanan6ac15442023-08-20 18:20:17 +0200263When defining the new() method the return type should not be specified. It
264always returns an object of the class.
265
Yegappan Lakshmananb90e3bc2023-09-28 23:06:48 +0200266 *E1386*
267When invoking an object method, the method name should be preceded by the
268object variable name. A object method cannot be invoked using the class
269name.
270
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000271==============================================================================
272
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02002733. Class Variables and Methods *Vim9-class-member*
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000274
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200275 *:static* *E1337* *E1338* *E1368*
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000276Class members are declared with "static". They are used by the name without a
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200277prefix in the class where they are defined: >
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000278
279 class OtherThing
280 this.size: number
281 static totalSize: number
282
283 def new(this.size)
284 totalSize += this.size
285 enddef
286 endclass
287< *E1340* *E1341*
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700288Since the name is used as-is, shadowing the name by a method argument name
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000289or local variable name is not allowed.
290
Yegappan Lakshmananb90e3bc2023-09-28 23:06:48 +0200291 *E1374* *E1375* *E1384* *E1385*
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200292To access a class member outside of the class where it is defined, the class
293name prefix must be used. A class member cannot be accessed using an object.
294
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000295Just like object members the access can be made private by using an underscore
296as the first character in the name, and it can be made public by prefixing
297"public": >
298
299 class OtherThing
300 static total: number # anybody can read, only class can write
301 static _sum: number # only class can read and write
302 public static result: number # anybody can read and write
303 endclass
304<
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200305 *class-method*
306Class methods are also declared with "static". They can use the class
307variables but they have no access to the object variables, they cannot use the
h_eastba77bbb2023-10-03 04:47:13 +0900308"this" keyword:
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200309>
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000310 class OtherThing
311 this.size: number
312 static totalSize: number
313
314 # Clear the total size and return the value it had before.
315 static def ClearTotalSize(): number
316 var prev = totalSize
317 totalSize = 0
318 return prev
319 enddef
320 endclass
321
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200322Inside the class the class method can be called by name directly, outside the
323class the class name must be prefixed: `OtherThing.ClearTotalSize()`. To use
324a super class method in a child class, the class name must be prefixed.
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000325
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +0200326Just like object methods the access can be made private by using an underscore
327as the first character in the method name: >
328
329 class OtherThing
330 static def _Foo()
331 echo "Foo"
332 enddef
333 def Bar()
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200334 _Foo()
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +0200335 enddef
336 endclass
Gianmaria Bajo4b9777a2023-08-29 22:26:30 +0200337<
338 *E1370*
339Note that constructors cannot be declared as "static", because they always
340are.
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +0200341
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200342To access the class methods and class variables of a super class in an
343extended class, the class name prefix should be used just as from anywhere
344outside of the defining class: >
345
346 vim9script
347 class Vehicle
348 static nextID: number = 1000
349 static def GetID(): number
350 nextID += 1
351 return nextID
352 enddef
353 endclass
354 class Car extends Vehicle
355 this.myID: number
356 def new()
357 this.myID = Vehicle.GetID()
358 enddef
359 endclass
360<
361Class variables and methods are not inherited by a child class. A child class
362can declare a static variable or a method with the same name as the one in the
363super class. Depending on the class where the member is used the
364corresponding class member will be used. The type of the class member in a
365child class can be different from that in the super class.
366
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000367==============================================================================
368
3694. Using an abstract class *Vim9-abstract-class*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000370
371An abstract class forms the base for at least one sub-class. In the class
372model one often finds that a few classes have the same properties that can be
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000373shared, but a class with these properties does not have enough state to create
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000374an object from. A sub-class must extend the abstract class and add the
375missing state and/or methods before it can be used to create objects for.
376
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000377For example, a Shape class could store a color and thickness. You cannot
378create a Shape object, it is missing the information about what kind of shape
379it is. The Shape class functions as the base for a Square and a Triangle
380class, for which objects can be created. Example: >
381
382 abstract class Shape
383 this.color = Color.Black
384 this.thickness = 10
385 endclass
386
387 class Square extends Shape
388 this.size: number
389
390 def new(this.size)
391 enddef
392 endclass
393
394 class Triangle extends Shape
395 this.base: number
396 this.height: number
397
398 def new(this.base, this.height)
399 enddef
400 endclass
401<
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000402An abstract class is defined the same way as a normal class, except that it
403does not have any new() method. *E1359*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000404
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200405 *abstract-method* *E1371* *E1372*
Yegappan Lakshmanan7bcd25c2023-09-08 19:27:51 +0200406An abstract method can be defined in an abstract class by using the "abstract"
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700407prefix when defining the method: >
Yegappan Lakshmanan7bcd25c2023-09-08 19:27:51 +0200408
409 abstract class Shape
410 abstract def Draw()
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200411 abstract static def SetColor()
Yegappan Lakshmanan7bcd25c2023-09-08 19:27:51 +0200412 endclass
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200413<
414 *E1373*
Yegappan Lakshmanan7bcd25c2023-09-08 19:27:51 +0200415A class extending the abstract class must implement all the abstract methods.
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200416The signature (arguments, argument types and return type) must be exactly the
Yegappan Lakshmanan26e8f7b2023-10-06 10:24:10 -0700417same. If the return type of a method is a class, then that class or one of
418its subclasses can be used in the extended method. Class methods in an
419abstract class can also be abstract methods.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000420
421==============================================================================
422
Bram Moolenaarbe4e0162023-02-02 13:59:48 +00004235. Using an interface *Vim9-using-interface*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000424
425The example above with Shape, Square and Triangle can be made more useful if
426we add a method to compute the surface of the object. For that we create the
427interface called HasSurface, which specifies one method Surface() that returns
428a number. This example extends the one above: >
429
430 abstract class Shape
431 this.color = Color.Black
432 this.thickness = 10
433 endclass
434
435 interface HasSurface
436 def Surface(): number
437 endinterface
438
439 class Square extends Shape implements HasSurface
440 this.size: number
441
442 def new(this.size)
443 enddef
444
445 def Surface(): number
446 return this.size * this.size
447 enddef
448 endclass
449
450 class Triangle extends Shape implements HasSurface
451 this.base: number
452 this.height: number
453
454 def new(this.base, this.height)
455 enddef
456
457 def Surface(): number
458 return this.base * this.height / 2
459 enddef
460 endclass
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200461<
462 *E1348* *E1349* *E1367* *E1382* *E1383*
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000463If a class declares to implement an interface, all the items specified in the
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200464interface must appear in the class, with the same types.
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000465
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000466The interface name can be used as a type: >
467
468 var shapes: list<HasSurface> = [
469 Square.new(12),
470 Triangle.new(8, 15),
471 ]
472 for shape in shapes
473 echo $'the surface is {shape.Surface()}'
474 endfor
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200475<
Yegappan Lakshmanan2dede3d2023-09-27 19:02:01 +0200476 *E1378* *E1379* *E1380* *E1387*
477An interface can contain only object methods and read-only object variables.
478An interface cannot contain read-write and private object variables, private
479object methods, class variables and class methods.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000480
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200481An interface can extend another interface using "extends". The sub-interface
482inherits all the instance variables and methods from the super interface.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000483
484==============================================================================
485
Bram Moolenaarbe4e0162023-02-02 13:59:48 +00004866. More class details *Vim9-class* *Class* *class*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000487
488Defining a class ~
489 *:class* *:endclass* *:abstract*
490A class is defined between `:class` and `:endclass`. The whole class is
491defined in one script file. It is not possible to add to a class later.
492
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000493A class can only be defined in a |Vim9| script file. *E1316*
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000494A class cannot be defined inside a function.
495
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000496It is possible to define more than one class in a script file. Although it
497usually is better to export only one main class. It can be useful to define
498types, enums and helper classes though.
499
500The `:abstract` keyword may be prefixed and `:export` may be used. That gives
501these variants: >
502
503 class ClassName
504 endclass
505
506 export class ClassName
507 endclass
508
509 abstract class ClassName
510 endclass
511
512 export abstract class ClassName
513 endclass
514<
515 *E1314*
516The class name should be CamelCased. It must start with an uppercase letter.
517That avoids clashing with builtin types.
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000518 *E1315*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000519After the class name these optional items can be used. Each can appear only
520once. They can appear in any order, although this order is recommended: >
521 extends ClassName
522 implements InterfaceName, OtherInterface
523 specifies SomeInterface
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200524< *E1355* *E1369*
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700525Each variable and method name can be used only once. It is not possible to
526define a method with the same name and different type of arguments. It is not
527possible to use a public and private member variable with the same name. A
528object variable name used in a super class cannot be reused in a child class.
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000529
530
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700531Object Variable Initialization ~
532If the type of a variable is not explicitly specified in a class, then it is
533set to "any" during class definition. When an object is instantiated from the
534class, then the type of the variable is set.
Yegappan Lakshmanan618e47d2023-08-22 21:29:28 +0200535
Yegappan Lakshmananf3b68d42023-09-29 22:50:02 +0200536The following reserved keyword names cannot be used as an object or class
537variable name: "super", "this", "true", "false", "null", "null_blob",
538"null_dict", "null_function", "null_list", "null_partial", "null_string",
539"null_channel" and "null_job".
Yegappan Lakshmanan618e47d2023-08-22 21:29:28 +0200540
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000541Extending a class ~
542 *extends*
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000543A class can extend one other class. *E1352* *E1353* *E1354*
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000544The basic idea is to build on top of an existing class, add properties to it.
545
546The extended class is called the "base class" or "super class". The new class
547is called the "child class".
548
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700549Object variables from the base class are all taken over by the child class. It
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000550is not possible to override them (unlike some other languages).
551
552 *E1356* *E1357* *E1358*
Yegappan Lakshmananb32064f2023-10-02 21:43:58 +0200553Object methods of the base class can be overruled. The signature (arguments,
Yegappan Lakshmanan26e8f7b2023-10-06 10:24:10 -0700554argument types and return type) must be exactly the same. If the return type
555of a method is a class, then that class or one of its subclasses can be used
556in the extended method. The method of the base class can be called by
557prefixing "super.".
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000558
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200559 *E1377*
560The access level of a method (public or private) in a child class should be
561the same as the super class.
562
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000563Other object methods of the base class are taken over by the child class.
564
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700565Class methods, including methods starting with "new", can be overruled, like
566with object methods. The method on the base class can be called by prefixing
567the name of the class (for class methods) or "super.".
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000568
569Unlike other languages, the constructor of the base class does not need to be
570invoked. In fact, it cannot be invoked. If some initialization from the base
571class also needs to be done in a child class, put it in an object method and
572call that method from every constructor().
573
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700574If the base class did not specify a new() method then one was automatically
575created. This method will not be taken over by the child class. The child
576class can define its own new() method, or, if there isn't one, a new() method
577will be added automatically.
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000578
579
580A class implementing an interface ~
Yegappan Lakshmanan2dede3d2023-09-27 19:02:01 +0200581 *implements* *E1346* *E1347* *E1389*
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000582A class can implement one or more interfaces. The "implements" keyword can
583only appear once *E1350* . Multiple interfaces can be specified, separated by
584commas. Each interface name can appear only once. *E1351*
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000585
586
587A class defining an interface ~
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000588 *specifies*
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700589A class can declare its interface, the object variables and methods, with a
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000590named interface. This avoids the need for separately specifying the
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000591interface, which is often done in many languages, especially Java.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000592
593
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000594Items in a class ~
Yegappan Lakshmanan2dede3d2023-09-27 19:02:01 +0200595 *E1318* *E1325* *E1388*
Bram Moolenaardd60c362023-02-27 15:49:53 +0000596Inside a class, in between `:class` and `:endclass`, these items can appear:
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700597- An object variable declaration: >
598 this._privateVariableName: memberType
599 this.readonlyVariableName: memberType
600 public this.readwriteVariableName: memberType
601- A class variable declaration: >
602 static _privateClassVariableName: memberType
603 static readonlyClassVariableName: memberType
604 static public readwriteClassVariableName: memberType
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000605- A constructor method: >
Bram Moolenaar938ae282023-02-20 20:44:55 +0000606 def new(arguments)
607 def newName(arguments)
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200608- A class method: >
609 static def SomeMethod(arguments)
610 static def _PrivateMethod(arguments)
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000611- An object method: >
Bram Moolenaar938ae282023-02-20 20:44:55 +0000612 def SomeMethod(arguments)
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200613 def _PrivateMethod(arguments)
614
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700615For the object variable the type must be specified. The best way is to do
616this explicitly with ": {type}". For simple types you can also use an
617initializer, such as "= 123", and Vim will see that the type is a number.
618Avoid doing this for more complex types and when the type will be incomplete.
619For example: >
Bram Moolenaarf1dcd142022-12-31 15:30:45 +0000620 this.nameList = []
621This specifies a list, but the item type is unknown. Better use: >
622 this.nameList: list<string>
623The initialization isn't needed, the list is empty by default.
624 *E1330*
625Some types cannot be used, such as "void", "null" and "v:none".
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000626
627
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000628Defining an interface ~
629 *:interface* *:endinterface*
630An interface is defined between `:interface` and `:endinterface`. It may be
631prefixed with `:export`: >
632
633 interface InterfaceName
634 endinterface
635
636 export interface InterfaceName
637 endinterface
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000638< *E1344*
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700639An interface can declare object variables, just like in a class but without
640any initializer.
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000641 *E1345*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000642An interface can declare methods with `:def`, including the arguments and
643return type, but without the body and without `:enddef`. Example: >
644
645 interface HasSurface
646 this.size: number
647 def Surface(): number
648 endinterface
649
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000650An interface name must start with an uppercase letter. *E1343*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000651The "Has" prefix can be used to make it easier to guess this is an interface
652name, with a hint about what it provides.
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000653An interface can only be defined in a |Vim9| script file. *E1342*
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200654An interface cannot "implement" another interface but it can "extend" another
655interface. *E1381*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000656
657
Bram Moolenaar938ae282023-02-20 20:44:55 +0000658null object ~
659
Bram Moolenaardd60c362023-02-27 15:49:53 +0000660When a variable is declared to have the type of an object, but it is not
Bram Moolenaar938ae282023-02-20 20:44:55 +0000661initialized, the value is null. When trying to use this null object Vim often
662does not know what class was supposed to be used. Vim then cannot check if
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700663a variable name is correct and you will get an "Using a null object" error,
h_eastba77bbb2023-10-03 04:47:13 +0900664even when the variable name is invalid. *E1360* *E1362*
Bram Moolenaar938ae282023-02-20 20:44:55 +0000665
666
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000667Default constructor ~
Yegappan Lakshmanan413f8392023-09-28 22:46:37 +0200668 *default-constructor*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000669In case you define a class without a new() method, one will be automatically
670defined. This default constructor will have arguments for all the object
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700671variables, in the order they were specified. Thus if your class looks like: >
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000672
673 class AutoNew
674 this.name: string
675 this.age: number
676 this.gender: Gender
677 endclass
678
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700679Then the default constructor will be: >
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000680
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000681 def new(this.name = v:none, this.age = v:none, this.gender = v:none)
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000682 enddef
683
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000684The "= v:none" default values make the arguments optional. Thus you can also
685call `new()` without any arguments. No assignment will happen and the default
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700686value for the object variables will be used. This is a more useful example,
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000687with default values: >
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000688
689 class TextPosition
690 this.lnum: number = 1
691 this.col: number = 1
692 endclass
693
694If you want the constructor to have mandatory arguments, you need to write it
695yourself. For example, if for the AutoNew class above you insist on getting
696the name, you can define the constructor like this: >
697
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000698 def new(this.name, this.age = v:none, this.gender = v:none)
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000699 enddef
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000700< *E1328*
701Note that you cannot use another default value than "v:none" here. If you
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700702want to initialize the object variables, do it where they are declared. This
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000703way you only need to look in one place for the default values.
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000704
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700705All object variables will be used in the default constructor, also private
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000706access ones.
707
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700708If the class extends another one, the object variables of that class will come
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000709first.
710
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000711
712Multiple constructors ~
713
714Normally a class has just one new() constructor. In case you find that the
715constructor is often called with the same arguments you may want to simplify
716your code by putting those arguments into a second constructor method. For
717example, if you tend to use the color black a lot: >
718
719 def new(this.garment, this.color, this.size)
720 enddef
721 ...
722 var pants = new(Garment.pants, Color.black, "XL")
723 var shirt = new(Garment.shirt, Color.black, "XL")
724 var shoes = new(Garment.shoes, Color.black, "45")
725
726Instead of repeating the color every time you can add a constructor that
727includes it: >
728
729 def newBlack(this.garment, this.size)
730 this.color = Color.black
731 enddef
732 ...
733 var pants = newBlack(Garment.pants, "XL")
734 var shirt = newBlack(Garment.shirt, "XL")
735 var shoes = newBlack(Garment.shoes, "9.5")
736
737Note that the method name must start with "new". If there is no method called
738"new()" then the default constructor is added, even though there are other
739constructor methods.
740
741
742==============================================================================
743
Bram Moolenaarbe4e0162023-02-02 13:59:48 +00007447. Type definition *Vim9-type* *:type*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000745
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +0200746A type definition is giving a name to a type specification. This also known
747type alias. For Example: >
Yegappan Lakshmanan26e8f7b2023-10-06 10:24:10 -0700748
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +0200749 :type ListOfStrings = list<string>
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000750
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +0200751The type alias can be used wherever a built-in type can be used. The type
752alias name must start with an upper case character.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000753
754==============================================================================
755
Bram Moolenaarbe4e0162023-02-02 13:59:48 +00007568. Enum *Vim9-enum* *:enum* *:endenum*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000757
Yegappan Lakshmanan26e8f7b2023-10-06 10:24:10 -0700758{not implemented yet}
759
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000760An enum is a type that can have one of a list of values. Example: >
761
762 :enum Color
763 White
764 Red
765 Green
766 Blue
767 Black
768 :endenum
769
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000770
771==============================================================================
772
7739. Rationale
774
775Most of the choices for |Vim9| classes come from popular and recently
776developed languages, such as Java, TypeScript and Dart. The syntax has been
777made to fit with the way Vim script works, such as using `endclass` instead of
778using curly braces around the whole class.
779
780Some common constructs of object-oriented languages were chosen very long ago
781when this kind of programming was still new, and later found to be
782sub-optimal. By this time those constructs were widely used and changing them
783was not an option. In Vim we do have the freedom to make different choices,
784since classes are completely new. We can make the syntax simpler and more
785consistent than what "old" languages use. Without diverting too much, it
786should still mostly look like what you know from existing languages.
787
788Some recently developed languages add all kinds of fancy features that we
789don't need for Vim. But some have nice ideas that we do want to use.
790Thus we end up with a base of what is common in popular languages, dropping
791what looks like a bad idea, and adding some nice features that are easy to
792understand.
793
794The main rules we use to make decisions:
795- Keep it simple.
796- No surprises, mostly do what other languages are doing.
797- Avoid mistakes from the past.
798- Avoid the need for the script writer to consult the help to understand how
799 things work, most things should be obvious.
800- Keep it consistent.
801- Aim at an average size plugin, not at a huge project.
802
803
804Using new() for the constructor ~
805
806Many languages use the class name for the constructor method. A disadvantage
807is that quite often this is a long name. And when changing the class name all
808constructor methods need to be renamed. Not a big deal, but still a
809disadvantage.
810
811Other languages, such as TypeScript, use a specific name, such as
812"constructor()". That seems better. However, using "new" or "new()" to
813create a new object has no obvious relation with "constructor()".
814
815For |Vim9| script using the same method name for all constructors seemed like
816the right choice, and by calling it new() the relation between the caller and
817the method being called is obvious.
818
819
820No overloading of the constructor ~
821
822In Vim script, both legacy and |Vim9| script, there is no overloading of
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700823methods. That means it is not possible to use the same method name with
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000824different types of arguments. Therefore there also is only one new()
825constructor.
826
827With |Vim9| script it would be possible to support overloading, since
828arguments are typed. However, this gets complicated very quickly. Looking at
829a new() call one has to inspect the types of the arguments to know which of
830several new() methods is actually being called. And that can require
831inspecting quite a bit of code. For example, if one of the arguments is the
832return value of a method, you need to find that method to see what type it is
833returning.
834
835Instead, every constructor has to have a different name, starting with "new".
836That way multiple constructors with different arguments are possible, while it
837is very easy to see which constructor is being used. And the type of
838arguments can be properly checked.
839
840
841No overloading of methods ~
842
843Same reasoning as for the constructor: It is often not obvious what type
844arguments have, which would make it difficult to figure out what method is
845actually being called. Better just give the methods a different name, then
846type checking will make sure it works as you intended. This rules out
847polymorphism, which we don't really need anyway.
848
849
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000850Single inheritance and interfaces ~
851
852Some languages support multiple inheritance. Although that can be useful in
853some cases, it makes the rules of how a class works quite complicated.
854Instead, using interfaces to declare what is supported is much simpler. The
855very popular Java language does it this way, and it should be good enough for
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000856Vim. The "keep it simple" rule applies here.
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000857
858Explicitly declaring that a class supports an interface makes it easy to see
859what a class is intended for. It also makes it possible to do proper type
860checking. When an interface is changed any class that declares to implement
861it will be checked if that change was also changed. The mechanism to assume a
862class implements an interface just because the methods happen to match is
863brittle and leads to obscure problems, let's not do that.
864
865
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700866Using "this.variable" everywhere ~
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000867
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700868The object variables in various programming languages can often be accessed in
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000869different ways, depending on the location. Sometimes "this." has to be
870prepended to avoid ambiguity. They are usually declared without "this.".
871That is quite inconsistent and sometimes confusing.
872
873A very common issue is that in the constructor the arguments use the same name
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700874as the object variable. Then for these variables "this." needs to be prefixed
875in the body, while for other variables this is not needed and often omitted.
876This leads to a mix of variables with and without "this.", which is
877inconsistent.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000878
879For |Vim9| classes the "this." prefix is always used. Also for declaring the
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700880variables. Simple and consistent. When looking at the code inside a class
881it's also directly clear which variable references are object variables and
882which aren't.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000883
884
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700885Using class variables ~
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000886
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700887Using "static variable" to declare a class variable is very common, nothing
888new here. In |Vim9| script these can be accessed directly by their name.
889Very much like how a script-local variable can be used in a method. Since
890object variables are always accessed with "this." prepended, it's also quickly
891clear what kind of variable it is.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000892
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700893TypeScript prepends the class name before the class variable name, also inside
894the class. This has two problems: The class name can be rather long, taking
895up quite a bit of space, and when the class is renamed all these places need
896to be changed too.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000897
898
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700899Declaring object and class variables ~
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000900
901The main choice is whether to use "var" as with variable declarations.
902TypeScript does not use it: >
903 class Point {
904 x: number;
905 y = 0;
906 }
907
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700908Following that Vim object variables could be declared like this: >
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000909 class Point
910 this.x: number
911 this.y = 0
912 endclass
913
914Some users pointed out that this looks more like an assignment than a
915declaration. Adding "var" changes that: >
916 class Point
917 var this.x: number
918 var this.y = 0
919 endclass
920
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700921We also need to be able to declare class variables using the "static" keyword.
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000922There we can also choose to leave out "var": >
923 class Point
924 var this.x: number
925 static count = 0
926 endclass
927
928Or do use it, before "static": >
929 class Point
930 var this.x: number
931 var static count = 0
932 endclass
933
934Or after "static": >
935 class Point
936 var this.x: number
937 static var count = 0
938 endclass
939
940This is more in line with "static def Func()".
941
942There is no clear preference whether to use "var" or not. The two main
943reasons to leave it out are:
9441. TypeScript, Java and other popular languages do not use it.
9452. Less clutter.
946
947
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000948Using "ClassName.new()" to construct an object ~
949
950Many languages use the "new" operator to create an object, which is actually
951kind of strange, since the constructor is defined as a method with arguments,
952not a command. TypeScript also has the "new" keyword, but the method is
953called "constructor()", it is hard to see the relation between the two.
954
955In |Vim9| script the constructor method is called new(), and it is invoked as
956new(), simple and straightforward. Other languages use "new ClassName()",
957while there is no ClassName() method, it's a method by another name in the
958class called ClassName. Quite confusing.
959
960
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700961Default read access to object variables ~
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000962
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700963Some users will remark that the access rules for object variables are
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000964asymmetric. Well, that is intentional. Changing a value is a very different
965action than reading a value. The read operation has no side effects, it can
966be done any number of times without affecting the object. Changing the value
967can have many side effects, and even have a ripple effect, affecting other
968objects.
969
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700970When adding object variables one usually doesn't think much about this, just
971get the type right. And normally the values are set in the new() method.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000972Therefore defaulting to read access only "just works" in most cases. And when
973directly writing you get an error, which makes you wonder if you actually want
974to allow that. This helps writing code with fewer mistakes.
975
976
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700977Making object variables private with an underscore ~
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000978
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700979When an object variable is private, it can only be read and changed inside the
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000980class (and in sub-classes), then it cannot be used outside of the class.
981Prepending an underscore is a simple way to make that visible. Various
982programming languages have this as a recommendation.
983
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700984In case you change your mind and want to make the object variable accessible
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000985outside of the class, you will have to remove the underscore everywhere.
986Since the name only appears in the class (and sub-classes) they will be easy
987to find and change.
988
989The other way around is much harder: you can easily prepend an underscore to
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700990the object variable inside the class to make it private, but any usage
991elsewhere you will have to track down and change. You may have to make it a
992"set" method call. This reflects the real world problem that taking away
993access requires work to be done for all places where that access exists.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000994
995An alternative would have been using the "private" keyword, just like "public"
996changes the access in the other direction. Well, that's just to reduce the
997number of keywords.
998
999
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -07001000No protected object variables ~
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001001
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -07001002Some languages provide several ways to control access to object variables.
1003The most known is "protected", and the meaning varies from language to
1004language. Others are "shared", "private" and even "friend".
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001005
1006These rules make life more difficult. That can be justified in projects where
1007many people work on the same, complex code where it is easy to make mistakes.
1008Especially when refactoring or other changes to the class model.
1009
1010The Vim scripts are expected to be used in a plugin, with just one person or a
1011small team working on it. Complex rules then only make it more complicated,
Bram Moolenaar71badf92023-04-22 22:40:14 +01001012the extra safety provided by the rules isn't really needed. Let's just keep
1013it simple and not specify access details.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001014
1015
1016==============================================================================
1017
101810. To be done later
1019
1020Can a newSomething() constructor invoke another constructor? If yes, what are
1021the restrictions?
1022
1023Thoughts:
1024- Generics for a class: `class <Tkey, Tentry>`
1025- Generics for a function: `def <Tkey> GetLast(key: Tkey)`
1026- Mixins: not sure if that is useful, leave out for simplicity.
1027
1028Some things that look like good additions:
1029- For testing: Mock mechanism
1030
1031An important class to be provided is "Promise". Since Vim is single
1032threaded, connecting asynchronous operations is a natural way of allowing
1033plugins to do their work without blocking the user. It's a uniform way to
1034invoke callbacks and handle timeouts and errors.
1035
1036
1037 vim:tw=78:ts=8:noet:ft=help:norl: