blob: 3e5493f20375ee69247f0c3cc7fc0ab7240a8d60 [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
31use this functionality effectively.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +000032
33The basic item is an object:
34- An object stores state. It contains one or more variables that can each
35 have a value.
Bram Moolenaarbe4e0162023-02-02 13:59:48 +000036- An object provides functions that use and manipulate its state. These
Bram Moolenaarc1c365c2022-12-04 20:13:24 +000037 functions are invoked "on the object", which is what sets it apart from the
38 traditional separation of data and code that manipulates the data.
39- An object has a well defined interface, with typed member variables and
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -070040 methods.
Bram Moolenaarbe4e0162023-02-02 13:59:48 +000041- Objects are created from a class and all objects have the same interface.
42 This does not change at runtime, it is not dynamic.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +000043
44An object can only be created by a class. A class provides:
45- A new() method, the constructor, which returns an object for the class.
46 This method is invoked on the class name: MyClass.new().
Bram Moolenaarbe4e0162023-02-02 13:59:48 +000047- State shared by all objects of the class: class variables (class members).
Bram Moolenaarc1c365c2022-12-04 20:13:24 +000048- A hierarchy of classes, with super-classes and sub-classes, inheritance.
49
50An interface is used to specify properties of an object:
51- An object can declare several interfaces that it implements.
52- Different objects implementing the same interface can be used the same way.
53
54The class hierarchy allows for single inheritance. Otherwise interfaces are
55to be used where needed.
56
57
58Class modeling ~
59
60You can model classes any way you like. Keep in mind what you are building,
61don't try to model the real world. This can be confusing, especially because
62teachers use real-world objects to explain class relations and you might think
63your model should therefore reflect the real world. It doesn't! The model
64should match your purpose.
65
Bram Moolenaarbe4e0162023-02-02 13:59:48 +000066Keep in mind that composition (an object contains other objects) is often
67better than inheritance (an object extends another object). Don't waste time
68trying to find the optimal class model. Or waste time discussing whether a
69square is a rectangle or that a rectangle is a square. It doesn't matter.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +000070
71
72==============================================================================
73
742. A simple class *Vim9-simple-class*
75
Bram Moolenaarbe4e0162023-02-02 13:59:48 +000076Let's start with a simple example: a class that stores a text position (see
77below for how to do this more efficiently): >
Bram Moolenaarc1c365c2022-12-04 20:13:24 +000078
79 class TextPosition
80 this.lnum: number
81 this.col: number
82
83 def new(lnum: number, col: number)
84 this.lnum = lnum
85 this.col = col
86 enddef
87
88 def SetLnum(lnum: number)
89 this.lnum = lnum
90 enddef
91
92 def SetCol(col: number)
93 this.col = col
94 enddef
95
96 def SetPosition(lnum: number, col: number)
97 this.lnum = lnum
98 this.col = col
99 enddef
100 endclass
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000101< *object* *Object*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000102You can create an object from this class with the new() method: >
103
104 var pos = TextPosition.new(1, 1)
105
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700106The object variables "lnum" and "col" can be accessed directly: >
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000107
108 echo $'The text position is ({pos.lnum}, {pos.col})'
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000109< *E1317* *E1327*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000110If you have been using other object-oriented languages you will notice that
111in Vim the object members are consistently referred to with the "this."
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000112prefix. This is different from languages like Java and TypeScript. The
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000113naming convention makes the object members easy to spot. Also, when a
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700114variable does not have the "this." prefix you know it is not an object
115variable.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000116
117
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700118Object variable write access ~
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000119
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700120Now try to change an object variable directly: >
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000121
122 pos.lnum = 9
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000123< *E1335*
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700124This will give you an error! That is because by default object variables can
125be read but not set. That's why the TextPosition class provides a method for
126it: >
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000127
128 pos.SetLnum(9)
129
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700130Allowing to read but not set an object variable is the most common and safest
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000131way. Most often there is no problem using a value, while setting a value may
132have side effects that need to be taken care of. In this case, the SetLnum()
133method could check if the line number is valid and either give an error or use
134the closest valid value.
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000135 *:public* *E1331*
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700136If you don't care about side effects and want to allow the object variable to
137be changed at any time, you can make it public: >
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000138
139 public this.lnum: number
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000140 public this.col: number
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000141
142Now you don't need the SetLnum(), SetCol() and SetPosition() methods, setting
143"pos.lnum" directly above will no longer give an error.
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200144 *E1326*
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700145If you try to set an object variable that doesn't exist you get an error: >
Bram Moolenaarf1dcd142022-12-31 15:30:45 +0000146 pos.other = 9
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200147< E1326: Member not found on object "TextPosition": other ~
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000148
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200149 *E1376*
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700150A object variable cannot be accessed using the class name.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000151
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700152Private variables ~
Yegappan Lakshmanan413f8392023-09-28 22:46:37 +0200153 *private-variable* *E1332* *E1333*
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700154On the other hand, if you do not want the object variables to be read directly,
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000155you can make them private. This is done by prefixing an underscore to the
156name: >
157
158 this._lnum: number
159 this._col number
160
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700161Now you need to provide methods to get the value of the private variables.
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000162These are commonly called getters. We recommend using a name that starts with
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000163"Get": >
164
165 def GetLnum(): number
166 return this._lnum
167 enddef
168
169 def GetCol() number
170 return this._col
171 enddef
172
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700173This example isn't very useful, the variables might as well have been public.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000174It does become useful if you check the value. For example, restrict the line
175number to the total number of lines: >
176
177 def GetLnum(): number
178 if this._lnum > this._lineCount
179 return this._lineCount
180 endif
181 return this._lnum
182 enddef
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200183<
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +0200184Private methods ~
Yegappan Lakshmanan413f8392023-09-28 22:46:37 +0200185 *private-method* *E1366*
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +0200186If you want object methods to be accessible only from other methods of the
187same class and not used from outside the class, then you can make them
188private. This is done by prefixing the method name with an underscore: >
189
190 class SomeClass
191 def _Foo(): number
192 return 10
193 enddef
194 def Bar(): number
195 return this._Foo()
196 enddef
197 endclass
198<
199Accessing a private method outside the class will result in an error (using
200the above class): >
201
202 var a = SomeClass.new()
203 a._Foo()
204<
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000205Simplifying the new() method ~
Yegappan Lakshmanan413f8392023-09-28 22:46:37 +0200206 *new()* *constructor*
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700207Many constructors take values for the object variables. Thus you very often
208see this pattern: >
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000209
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000210 class SomeClass
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000211 this.lnum: number
212 this.col: number
213
214 def new(lnum: number, col: number)
215 this.lnum = lnum
216 this.col = col
217 enddef
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000218 endclass
h-eastdb385522023-09-28 22:18:19 +0200219<
220 *E1390*
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700221Not only is this text you need to write, it also has the type of each
222variables twice. Since this is so common a shorter way to write new() is
223provided: >
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000224
225 def new(this.lnum, this.col)
226 enddef
227
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700228The semantics are easy to understand: Providing the object variable name,
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000229including "this.", as the argument to new() means the value provided in the
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700230new() call is assigned to that object variable. This mechanism comes from the
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000231Dart language.
232
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700233Putting together this way of using new() and making the variables public
234results in a much shorter class definition than what we started with: >
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000235
236 class TextPosition
237 public this.lnum: number
238 public this.col: number
239
240 def new(this.lnum, this.col)
241 enddef
242
243 def SetPosition(lnum: number, col: number)
244 this.lnum = lnum
245 this.col = col
246 enddef
247 endclass
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000248
249The sequence of constructing a new object is:
2501. Memory is allocated and cleared. All values are zero/false/empty.
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -07002512. For each declared object variable that has an initializer, the expression
252 is evaluated and assigned to the variable. This happens in the sequence
253 the variables are declared in the class.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00002543. Arguments in the new() method in the "this.name" form are assigned.
2554. The body of the new() method is executed.
256
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000257If the class extends a parent class, the same thing happens. In the second
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700258step the object variables of the parent class are initialized first. There is
259no need to call "super()" or "new()" on the parent.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000260
Yegappan Lakshmananb90e3bc2023-09-28 23:06:48 +0200261 *E1365*
Yegappan Lakshmanan6ac15442023-08-20 18:20:17 +0200262When defining the new() method the return type should not be specified. It
263always returns an object of the class.
264
Yegappan Lakshmananb90e3bc2023-09-28 23:06:48 +0200265 *E1386*
266When invoking an object method, the method name should be preceded by the
267object variable name. A object method cannot be invoked using the class
268name.
269
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000270==============================================================================
271
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02002723. Class Variables and Methods *Vim9-class-member*
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000273
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200274 *:static* *E1337* *E1338* *E1368*
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000275Class members are declared with "static". They are used by the name without a
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200276prefix in the class where they are defined: >
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000277
278 class OtherThing
279 this.size: number
280 static totalSize: number
281
282 def new(this.size)
283 totalSize += this.size
284 enddef
285 endclass
286< *E1340* *E1341*
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700287Since the name is used as-is, shadowing the name by a method argument name
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000288or local variable name is not allowed.
289
Yegappan Lakshmananb90e3bc2023-09-28 23:06:48 +0200290 *E1374* *E1375* *E1384* *E1385*
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200291To access a class member outside of the class where it is defined, the class
292name prefix must be used. A class member cannot be accessed using an object.
293
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000294Just like object members the access can be made private by using an underscore
295as the first character in the name, and it can be made public by prefixing
296"public": >
297
298 class OtherThing
299 static total: number # anybody can read, only class can write
300 static _sum: number # only class can read and write
301 public static result: number # anybody can read and write
302 endclass
303<
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200304 *class-method*
305Class methods are also declared with "static". They can use the class
306variables but they have no access to the object variables, they cannot use the
h_eastba77bbb2023-10-03 04:47:13 +0900307"this" keyword:
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200308>
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000309 class OtherThing
310 this.size: number
311 static totalSize: number
312
313 # Clear the total size and return the value it had before.
314 static def ClearTotalSize(): number
315 var prev = totalSize
316 totalSize = 0
317 return prev
318 enddef
319 endclass
320
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200321Inside the class the class method can be called by name directly, outside the
322class the class name must be prefixed: `OtherThing.ClearTotalSize()`. To use
323a super class method in a child class, the class name must be prefixed.
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000324
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +0200325Just like object methods the access can be made private by using an underscore
326as the first character in the method name: >
327
328 class OtherThing
329 static def _Foo()
330 echo "Foo"
331 enddef
332 def Bar()
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200333 _Foo()
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +0200334 enddef
335 endclass
Gianmaria Bajo4b9777a2023-08-29 22:26:30 +0200336<
337 *E1370*
338Note that constructors cannot be declared as "static", because they always
339are.
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +0200340
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200341To access the class methods and class variables of a super class in an
342extended class, the class name prefix should be used just as from anywhere
343outside of the defining class: >
344
345 vim9script
346 class Vehicle
347 static nextID: number = 1000
348 static def GetID(): number
349 nextID += 1
350 return nextID
351 enddef
352 endclass
353 class Car extends Vehicle
354 this.myID: number
355 def new()
356 this.myID = Vehicle.GetID()
357 enddef
358 endclass
359<
360Class variables and methods are not inherited by a child class. A child class
361can declare a static variable or a method with the same name as the one in the
362super class. Depending on the class where the member is used the
363corresponding class member will be used. The type of the class member in a
364child class can be different from that in the super class.
365
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000366==============================================================================
367
3684. Using an abstract class *Vim9-abstract-class*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000369
370An abstract class forms the base for at least one sub-class. In the class
371model one often finds that a few classes have the same properties that can be
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000372shared, but a class with these properties does not have enough state to create
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000373an object from. A sub-class must extend the abstract class and add the
374missing state and/or methods before it can be used to create objects for.
375
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000376For example, a Shape class could store a color and thickness. You cannot
377create a Shape object, it is missing the information about what kind of shape
378it is. The Shape class functions as the base for a Square and a Triangle
379class, for which objects can be created. Example: >
380
381 abstract class Shape
382 this.color = Color.Black
383 this.thickness = 10
384 endclass
385
386 class Square extends Shape
387 this.size: number
388
389 def new(this.size)
390 enddef
391 endclass
392
393 class Triangle extends Shape
394 this.base: number
395 this.height: number
396
397 def new(this.base, this.height)
398 enddef
399 endclass
400<
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000401An abstract class is defined the same way as a normal class, except that it
402does not have any new() method. *E1359*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000403
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200404 *abstract-method* *E1371* *E1372*
Yegappan Lakshmanan7bcd25c2023-09-08 19:27:51 +0200405An abstract method can be defined in an abstract class by using the "abstract"
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700406prefix when defining the method: >
Yegappan Lakshmanan7bcd25c2023-09-08 19:27:51 +0200407
408 abstract class Shape
409 abstract def Draw()
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200410 abstract static def SetColor()
Yegappan Lakshmanan7bcd25c2023-09-08 19:27:51 +0200411 endclass
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200412<
413 *E1373*
Yegappan Lakshmanan7bcd25c2023-09-08 19:27:51 +0200414A class extending the abstract class must implement all the abstract methods.
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200415The signature (arguments, argument types and return type) must be exactly the
416same. Class methods in an abstract class can also be abstract methods.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000417
418==============================================================================
419
Bram Moolenaarbe4e0162023-02-02 13:59:48 +00004205. Using an interface *Vim9-using-interface*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000421
422The example above with Shape, Square and Triangle can be made more useful if
423we add a method to compute the surface of the object. For that we create the
424interface called HasSurface, which specifies one method Surface() that returns
425a number. This example extends the one above: >
426
427 abstract class Shape
428 this.color = Color.Black
429 this.thickness = 10
430 endclass
431
432 interface HasSurface
433 def Surface(): number
434 endinterface
435
436 class Square extends Shape implements HasSurface
437 this.size: number
438
439 def new(this.size)
440 enddef
441
442 def Surface(): number
443 return this.size * this.size
444 enddef
445 endclass
446
447 class Triangle extends Shape implements HasSurface
448 this.base: number
449 this.height: number
450
451 def new(this.base, this.height)
452 enddef
453
454 def Surface(): number
455 return this.base * this.height / 2
456 enddef
457 endclass
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200458<
459 *E1348* *E1349* *E1367* *E1382* *E1383*
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000460If a class declares to implement an interface, all the items specified in the
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200461interface must appear in the class, with the same types.
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000462
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000463The interface name can be used as a type: >
464
465 var shapes: list<HasSurface> = [
466 Square.new(12),
467 Triangle.new(8, 15),
468 ]
469 for shape in shapes
470 echo $'the surface is {shape.Surface()}'
471 endfor
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200472<
Yegappan Lakshmanan2dede3d2023-09-27 19:02:01 +0200473 *E1378* *E1379* *E1380* *E1387*
474An interface can contain only object methods and read-only object variables.
475An interface cannot contain read-write and private object variables, private
476object methods, class variables and class methods.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000477
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200478An interface can extend another interface using "extends". The sub-interface
479inherits all the instance variables and methods from the super interface.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000480
481==============================================================================
482
Bram Moolenaarbe4e0162023-02-02 13:59:48 +00004836. More class details *Vim9-class* *Class* *class*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000484
485Defining a class ~
486 *:class* *:endclass* *:abstract*
487A class is defined between `:class` and `:endclass`. The whole class is
488defined in one script file. It is not possible to add to a class later.
489
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000490A class can only be defined in a |Vim9| script file. *E1316*
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000491A class cannot be defined inside a function.
492
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000493It is possible to define more than one class in a script file. Although it
494usually is better to export only one main class. It can be useful to define
495types, enums and helper classes though.
496
497The `:abstract` keyword may be prefixed and `:export` may be used. That gives
498these variants: >
499
500 class ClassName
501 endclass
502
503 export class ClassName
504 endclass
505
506 abstract class ClassName
507 endclass
508
509 export abstract class ClassName
510 endclass
511<
512 *E1314*
513The class name should be CamelCased. It must start with an uppercase letter.
514That avoids clashing with builtin types.
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000515 *E1315*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000516After the class name these optional items can be used. Each can appear only
517once. They can appear in any order, although this order is recommended: >
518 extends ClassName
519 implements InterfaceName, OtherInterface
520 specifies SomeInterface
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200521< *E1355* *E1369*
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700522Each variable and method name can be used only once. It is not possible to
523define a method with the same name and different type of arguments. It is not
524possible to use a public and private member variable with the same name. A
525object variable name used in a super class cannot be reused in a child class.
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000526
527
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700528Object Variable Initialization ~
529If the type of a variable is not explicitly specified in a class, then it is
530set to "any" during class definition. When an object is instantiated from the
531class, then the type of the variable is set.
Yegappan Lakshmanan618e47d2023-08-22 21:29:28 +0200532
Yegappan Lakshmananf3b68d42023-09-29 22:50:02 +0200533The following reserved keyword names cannot be used as an object or class
534variable name: "super", "this", "true", "false", "null", "null_blob",
535"null_dict", "null_function", "null_list", "null_partial", "null_string",
536"null_channel" and "null_job".
Yegappan Lakshmanan618e47d2023-08-22 21:29:28 +0200537
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000538Extending a class ~
539 *extends*
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000540A class can extend one other class. *E1352* *E1353* *E1354*
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000541The basic idea is to build on top of an existing class, add properties to it.
542
543The extended class is called the "base class" or "super class". The new class
544is called the "child class".
545
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700546Object variables from the base class are all taken over by the child class. It
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000547is not possible to override them (unlike some other languages).
548
549 *E1356* *E1357* *E1358*
Yegappan Lakshmananb32064f2023-10-02 21:43:58 +0200550Object methods of the base class can be overruled. The signature (arguments,
551argument types and return type) must be exactly the same. The method of the
552base class can be called by prefixing "super.".
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000553
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200554 *E1377*
555The access level of a method (public or private) in a child class should be
556the same as the super class.
557
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000558Other object methods of the base class are taken over by the child class.
559
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700560Class methods, including methods starting with "new", can be overruled, like
561with object methods. The method on the base class can be called by prefixing
562the name of the class (for class methods) or "super.".
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000563
564Unlike other languages, the constructor of the base class does not need to be
565invoked. In fact, it cannot be invoked. If some initialization from the base
566class also needs to be done in a child class, put it in an object method and
567call that method from every constructor().
568
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700569If the base class did not specify a new() method then one was automatically
570created. This method will not be taken over by the child class. The child
571class can define its own new() method, or, if there isn't one, a new() method
572will be added automatically.
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000573
574
575A class implementing an interface ~
Yegappan Lakshmanan2dede3d2023-09-27 19:02:01 +0200576 *implements* *E1346* *E1347* *E1389*
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000577A class can implement one or more interfaces. The "implements" keyword can
578only appear once *E1350* . Multiple interfaces can be specified, separated by
579commas. Each interface name can appear only once. *E1351*
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000580
581
582A class defining an interface ~
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000583 *specifies*
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700584A class can declare its interface, the object variables and methods, with a
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000585named interface. This avoids the need for separately specifying the
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000586interface, which is often done in many languages, especially Java.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000587
588
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000589Items in a class ~
Yegappan Lakshmanan2dede3d2023-09-27 19:02:01 +0200590 *E1318* *E1325* *E1388*
Bram Moolenaardd60c362023-02-27 15:49:53 +0000591Inside a class, in between `:class` and `:endclass`, these items can appear:
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700592- An object variable declaration: >
593 this._privateVariableName: memberType
594 this.readonlyVariableName: memberType
595 public this.readwriteVariableName: memberType
596- A class variable declaration: >
597 static _privateClassVariableName: memberType
598 static readonlyClassVariableName: memberType
599 static public readwriteClassVariableName: memberType
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000600- A constructor method: >
Bram Moolenaar938ae282023-02-20 20:44:55 +0000601 def new(arguments)
602 def newName(arguments)
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200603- A class method: >
604 static def SomeMethod(arguments)
605 static def _PrivateMethod(arguments)
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000606- An object method: >
Bram Moolenaar938ae282023-02-20 20:44:55 +0000607 def SomeMethod(arguments)
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200608 def _PrivateMethod(arguments)
609
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700610For the object variable the type must be specified. The best way is to do
611this explicitly with ": {type}". For simple types you can also use an
612initializer, such as "= 123", and Vim will see that the type is a number.
613Avoid doing this for more complex types and when the type will be incomplete.
614For example: >
Bram Moolenaarf1dcd142022-12-31 15:30:45 +0000615 this.nameList = []
616This specifies a list, but the item type is unknown. Better use: >
617 this.nameList: list<string>
618The initialization isn't needed, the list is empty by default.
619 *E1330*
620Some types cannot be used, such as "void", "null" and "v:none".
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000621
622
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000623Defining an interface ~
624 *:interface* *:endinterface*
625An interface is defined between `:interface` and `:endinterface`. It may be
626prefixed with `:export`: >
627
628 interface InterfaceName
629 endinterface
630
631 export interface InterfaceName
632 endinterface
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000633< *E1344*
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700634An interface can declare object variables, just like in a class but without
635any initializer.
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000636 *E1345*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000637An interface can declare methods with `:def`, including the arguments and
638return type, but without the body and without `:enddef`. Example: >
639
640 interface HasSurface
641 this.size: number
642 def Surface(): number
643 endinterface
644
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000645An interface name must start with an uppercase letter. *E1343*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000646The "Has" prefix can be used to make it easier to guess this is an interface
647name, with a hint about what it provides.
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000648An interface can only be defined in a |Vim9| script file. *E1342*
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200649An interface cannot "implement" another interface but it can "extend" another
650interface. *E1381*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000651
652
Bram Moolenaar938ae282023-02-20 20:44:55 +0000653null object ~
654
Bram Moolenaardd60c362023-02-27 15:49:53 +0000655When a variable is declared to have the type of an object, but it is not
Bram Moolenaar938ae282023-02-20 20:44:55 +0000656initialized, the value is null. When trying to use this null object Vim often
657does not know what class was supposed to be used. Vim then cannot check if
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700658a variable name is correct and you will get an "Using a null object" error,
h_eastba77bbb2023-10-03 04:47:13 +0900659even when the variable name is invalid. *E1360* *E1362*
Bram Moolenaar938ae282023-02-20 20:44:55 +0000660
661
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000662Default constructor ~
Yegappan Lakshmanan413f8392023-09-28 22:46:37 +0200663 *default-constructor*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000664In case you define a class without a new() method, one will be automatically
665defined. This default constructor will have arguments for all the object
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700666variables, in the order they were specified. Thus if your class looks like: >
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000667
668 class AutoNew
669 this.name: string
670 this.age: number
671 this.gender: Gender
672 endclass
673
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700674Then the default constructor will be: >
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000675
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000676 def new(this.name = v:none, this.age = v:none, this.gender = v:none)
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000677 enddef
678
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000679The "= v:none" default values make the arguments optional. Thus you can also
680call `new()` without any arguments. No assignment will happen and the default
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700681value for the object variables will be used. This is a more useful example,
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000682with default values: >
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000683
684 class TextPosition
685 this.lnum: number = 1
686 this.col: number = 1
687 endclass
688
689If you want the constructor to have mandatory arguments, you need to write it
690yourself. For example, if for the AutoNew class above you insist on getting
691the name, you can define the constructor like this: >
692
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000693 def new(this.name, this.age = v:none, this.gender = v:none)
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000694 enddef
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000695< *E1328*
696Note that you cannot use another default value than "v:none" here. If you
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700697want to initialize the object variables, do it where they are declared. This
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000698way you only need to look in one place for the default values.
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000699
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700700All object variables will be used in the default constructor, also private
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000701access ones.
702
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700703If the class extends another one, the object variables of that class will come
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000704first.
705
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000706
707Multiple constructors ~
708
709Normally a class has just one new() constructor. In case you find that the
710constructor is often called with the same arguments you may want to simplify
711your code by putting those arguments into a second constructor method. For
712example, if you tend to use the color black a lot: >
713
714 def new(this.garment, this.color, this.size)
715 enddef
716 ...
717 var pants = new(Garment.pants, Color.black, "XL")
718 var shirt = new(Garment.shirt, Color.black, "XL")
719 var shoes = new(Garment.shoes, Color.black, "45")
720
721Instead of repeating the color every time you can add a constructor that
722includes it: >
723
724 def newBlack(this.garment, this.size)
725 this.color = Color.black
726 enddef
727 ...
728 var pants = newBlack(Garment.pants, "XL")
729 var shirt = newBlack(Garment.shirt, "XL")
730 var shoes = newBlack(Garment.shoes, "9.5")
731
732Note that the method name must start with "new". If there is no method called
733"new()" then the default constructor is added, even though there are other
734constructor methods.
735
736
737==============================================================================
738
Bram Moolenaarbe4e0162023-02-02 13:59:48 +00007397. Type definition *Vim9-type* *:type*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000740
741A type definition is giving a name to a type specification. For Example: >
742
743 :type ListOfStrings list<string>
744
745TODO: more explanation
746
747
748==============================================================================
749
Bram Moolenaarbe4e0162023-02-02 13:59:48 +00007508. Enum *Vim9-enum* *:enum* *:endenum*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000751
752An enum is a type that can have one of a list of values. Example: >
753
754 :enum Color
755 White
756 Red
757 Green
758 Blue
759 Black
760 :endenum
761
762TODO: more explanation
763
764
765==============================================================================
766
7679. Rationale
768
769Most of the choices for |Vim9| classes come from popular and recently
770developed languages, such as Java, TypeScript and Dart. The syntax has been
771made to fit with the way Vim script works, such as using `endclass` instead of
772using curly braces around the whole class.
773
774Some common constructs of object-oriented languages were chosen very long ago
775when this kind of programming was still new, and later found to be
776sub-optimal. By this time those constructs were widely used and changing them
777was not an option. In Vim we do have the freedom to make different choices,
778since classes are completely new. We can make the syntax simpler and more
779consistent than what "old" languages use. Without diverting too much, it
780should still mostly look like what you know from existing languages.
781
782Some recently developed languages add all kinds of fancy features that we
783don't need for Vim. But some have nice ideas that we do want to use.
784Thus we end up with a base of what is common in popular languages, dropping
785what looks like a bad idea, and adding some nice features that are easy to
786understand.
787
788The main rules we use to make decisions:
789- Keep it simple.
790- No surprises, mostly do what other languages are doing.
791- Avoid mistakes from the past.
792- Avoid the need for the script writer to consult the help to understand how
793 things work, most things should be obvious.
794- Keep it consistent.
795- Aim at an average size plugin, not at a huge project.
796
797
798Using new() for the constructor ~
799
800Many languages use the class name for the constructor method. A disadvantage
801is that quite often this is a long name. And when changing the class name all
802constructor methods need to be renamed. Not a big deal, but still a
803disadvantage.
804
805Other languages, such as TypeScript, use a specific name, such as
806"constructor()". That seems better. However, using "new" or "new()" to
807create a new object has no obvious relation with "constructor()".
808
809For |Vim9| script using the same method name for all constructors seemed like
810the right choice, and by calling it new() the relation between the caller and
811the method being called is obvious.
812
813
814No overloading of the constructor ~
815
816In Vim script, both legacy and |Vim9| script, there is no overloading of
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700817methods. That means it is not possible to use the same method name with
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000818different types of arguments. Therefore there also is only one new()
819constructor.
820
821With |Vim9| script it would be possible to support overloading, since
822arguments are typed. However, this gets complicated very quickly. Looking at
823a new() call one has to inspect the types of the arguments to know which of
824several new() methods is actually being called. And that can require
825inspecting quite a bit of code. For example, if one of the arguments is the
826return value of a method, you need to find that method to see what type it is
827returning.
828
829Instead, every constructor has to have a different name, starting with "new".
830That way multiple constructors with different arguments are possible, while it
831is very easy to see which constructor is being used. And the type of
832arguments can be properly checked.
833
834
835No overloading of methods ~
836
837Same reasoning as for the constructor: It is often not obvious what type
838arguments have, which would make it difficult to figure out what method is
839actually being called. Better just give the methods a different name, then
840type checking will make sure it works as you intended. This rules out
841polymorphism, which we don't really need anyway.
842
843
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000844Single inheritance and interfaces ~
845
846Some languages support multiple inheritance. Although that can be useful in
847some cases, it makes the rules of how a class works quite complicated.
848Instead, using interfaces to declare what is supported is much simpler. The
849very popular Java language does it this way, and it should be good enough for
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000850Vim. The "keep it simple" rule applies here.
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000851
852Explicitly declaring that a class supports an interface makes it easy to see
853what a class is intended for. It also makes it possible to do proper type
854checking. When an interface is changed any class that declares to implement
855it will be checked if that change was also changed. The mechanism to assume a
856class implements an interface just because the methods happen to match is
857brittle and leads to obscure problems, let's not do that.
858
859
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700860Using "this.variable" everywhere ~
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000861
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700862The object variables in various programming languages can often be accessed in
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000863different ways, depending on the location. Sometimes "this." has to be
864prepended to avoid ambiguity. They are usually declared without "this.".
865That is quite inconsistent and sometimes confusing.
866
867A very common issue is that in the constructor the arguments use the same name
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700868as the object variable. Then for these variables "this." needs to be prefixed
869in the body, while for other variables this is not needed and often omitted.
870This leads to a mix of variables with and without "this.", which is
871inconsistent.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000872
873For |Vim9| classes the "this." prefix is always used. Also for declaring the
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700874variables. Simple and consistent. When looking at the code inside a class
875it's also directly clear which variable references are object variables and
876which aren't.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000877
878
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700879Using class variables ~
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000880
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700881Using "static variable" to declare a class variable is very common, nothing
882new here. In |Vim9| script these can be accessed directly by their name.
883Very much like how a script-local variable can be used in a method. Since
884object variables are always accessed with "this." prepended, it's also quickly
885clear what kind of variable it is.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000886
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700887TypeScript prepends the class name before the class variable name, also inside
888the class. This has two problems: The class name can be rather long, taking
889up quite a bit of space, and when the class is renamed all these places need
890to be changed too.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000891
892
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700893Declaring object and class variables ~
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000894
895The main choice is whether to use "var" as with variable declarations.
896TypeScript does not use it: >
897 class Point {
898 x: number;
899 y = 0;
900 }
901
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700902Following that Vim object variables could be declared like this: >
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000903 class Point
904 this.x: number
905 this.y = 0
906 endclass
907
908Some users pointed out that this looks more like an assignment than a
909declaration. Adding "var" changes that: >
910 class Point
911 var this.x: number
912 var this.y = 0
913 endclass
914
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700915We also need to be able to declare class variables using the "static" keyword.
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000916There we can also choose to leave out "var": >
917 class Point
918 var this.x: number
919 static count = 0
920 endclass
921
922Or do use it, before "static": >
923 class Point
924 var this.x: number
925 var static count = 0
926 endclass
927
928Or after "static": >
929 class Point
930 var this.x: number
931 static var count = 0
932 endclass
933
934This is more in line with "static def Func()".
935
936There is no clear preference whether to use "var" or not. The two main
937reasons to leave it out are:
9381. TypeScript, Java and other popular languages do not use it.
9392. Less clutter.
940
941
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000942Using "ClassName.new()" to construct an object ~
943
944Many languages use the "new" operator to create an object, which is actually
945kind of strange, since the constructor is defined as a method with arguments,
946not a command. TypeScript also has the "new" keyword, but the method is
947called "constructor()", it is hard to see the relation between the two.
948
949In |Vim9| script the constructor method is called new(), and it is invoked as
950new(), simple and straightforward. Other languages use "new ClassName()",
951while there is no ClassName() method, it's a method by another name in the
952class called ClassName. Quite confusing.
953
954
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700955Default read access to object variables ~
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000956
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700957Some users will remark that the access rules for object variables are
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000958asymmetric. Well, that is intentional. Changing a value is a very different
959action than reading a value. The read operation has no side effects, it can
960be done any number of times without affecting the object. Changing the value
961can have many side effects, and even have a ripple effect, affecting other
962objects.
963
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700964When adding object variables one usually doesn't think much about this, just
965get the type right. And normally the values are set in the new() method.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000966Therefore defaulting to read access only "just works" in most cases. And when
967directly writing you get an error, which makes you wonder if you actually want
968to allow that. This helps writing code with fewer mistakes.
969
970
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700971Making object variables private with an underscore ~
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000972
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700973When an object variable is private, it can only be read and changed inside the
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000974class (and in sub-classes), then it cannot be used outside of the class.
975Prepending an underscore is a simple way to make that visible. Various
976programming languages have this as a recommendation.
977
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700978In case you change your mind and want to make the object variable accessible
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000979outside of the class, you will have to remove the underscore everywhere.
980Since the name only appears in the class (and sub-classes) they will be easy
981to find and change.
982
983The other way around is much harder: you can easily prepend an underscore to
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700984the object variable inside the class to make it private, but any usage
985elsewhere you will have to track down and change. You may have to make it a
986"set" method call. This reflects the real world problem that taking away
987access requires work to be done for all places where that access exists.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000988
989An alternative would have been using the "private" keyword, just like "public"
990changes the access in the other direction. Well, that's just to reduce the
991number of keywords.
992
993
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700994No protected object variables ~
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000995
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700996Some languages provide several ways to control access to object variables.
997The most known is "protected", and the meaning varies from language to
998language. Others are "shared", "private" and even "friend".
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000999
1000These rules make life more difficult. That can be justified in projects where
1001many people work on the same, complex code where it is easy to make mistakes.
1002Especially when refactoring or other changes to the class model.
1003
1004The Vim scripts are expected to be used in a plugin, with just one person or a
1005small team working on it. Complex rules then only make it more complicated,
Bram Moolenaar71badf92023-04-22 22:40:14 +01001006the extra safety provided by the rules isn't really needed. Let's just keep
1007it simple and not specify access details.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001008
1009
1010==============================================================================
1011
101210. To be done later
1013
1014Can a newSomething() constructor invoke another constructor? If yes, what are
1015the restrictions?
1016
1017Thoughts:
1018- Generics for a class: `class <Tkey, Tentry>`
1019- Generics for a function: `def <Tkey> GetLast(key: Tkey)`
1020- Mixins: not sure if that is useful, leave out for simplicity.
1021
1022Some things that look like good additions:
1023- For testing: Mock mechanism
1024
1025An important class to be provided is "Promise". Since Vim is single
1026threaded, connecting asynchronous operations is a natural way of allowing
1027plugins to do their work without blocking the user. It's a uniform way to
1028invoke callbacks and handle timeouts and errors.
1029
1030
1031 vim:tw=78:ts=8:noet:ft=help:norl: