blob: 6b51e86b6fdd6d60b81d0f5d1c89ad1ff74edaac [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
307"this" keyword.
308>
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
533
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000534Extending a class ~
535 *extends*
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000536A class can extend one other class. *E1352* *E1353* *E1354*
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000537The basic idea is to build on top of an existing class, add properties to it.
538
539The extended class is called the "base class" or "super class". The new class
540is called the "child class".
541
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700542Object variables from the base class are all taken over by the child class. It
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000543is not possible to override them (unlike some other languages).
544
545 *E1356* *E1357* *E1358*
546Object methods of the base class can be overruled. The signature (arguments,
547argument types and return type) must be exactly the same. The method of the
548base class can be called by prefixing "super.".
549
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200550 *E1377*
551The access level of a method (public or private) in a child class should be
552the same as the super class.
553
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000554Other object methods of the base class are taken over by the child class.
555
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700556Class methods, including methods starting with "new", can be overruled, like
557with object methods. The method on the base class can be called by prefixing
558the name of the class (for class methods) or "super.".
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000559
560Unlike other languages, the constructor of the base class does not need to be
561invoked. In fact, it cannot be invoked. If some initialization from the base
562class also needs to be done in a child class, put it in an object method and
563call that method from every constructor().
564
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700565If the base class did not specify a new() method then one was automatically
566created. This method will not be taken over by the child class. The child
567class can define its own new() method, or, if there isn't one, a new() method
568will be added automatically.
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000569
570
571A class implementing an interface ~
Yegappan Lakshmanan2dede3d2023-09-27 19:02:01 +0200572 *implements* *E1346* *E1347* *E1389*
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000573A class can implement one or more interfaces. The "implements" keyword can
574only appear once *E1350* . Multiple interfaces can be specified, separated by
575commas. Each interface name can appear only once. *E1351*
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000576
577
578A class defining an interface ~
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000579 *specifies*
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700580A class can declare its interface, the object variables and methods, with a
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000581named interface. This avoids the need for separately specifying the
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000582interface, which is often done in many languages, especially Java.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000583
584
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000585Items in a class ~
Yegappan Lakshmanan2dede3d2023-09-27 19:02:01 +0200586 *E1318* *E1325* *E1388*
Bram Moolenaardd60c362023-02-27 15:49:53 +0000587Inside a class, in between `:class` and `:endclass`, these items can appear:
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700588- An object variable declaration: >
589 this._privateVariableName: memberType
590 this.readonlyVariableName: memberType
591 public this.readwriteVariableName: memberType
592- A class variable declaration: >
593 static _privateClassVariableName: memberType
594 static readonlyClassVariableName: memberType
595 static public readwriteClassVariableName: memberType
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000596- A constructor method: >
Bram Moolenaar938ae282023-02-20 20:44:55 +0000597 def new(arguments)
598 def newName(arguments)
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200599- A class method: >
600 static def SomeMethod(arguments)
601 static def _PrivateMethod(arguments)
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000602- An object method: >
Bram Moolenaar938ae282023-02-20 20:44:55 +0000603 def SomeMethod(arguments)
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200604 def _PrivateMethod(arguments)
605
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700606For the object variable the type must be specified. The best way is to do
607this explicitly with ": {type}". For simple types you can also use an
608initializer, such as "= 123", and Vim will see that the type is a number.
609Avoid doing this for more complex types and when the type will be incomplete.
610For example: >
Bram Moolenaarf1dcd142022-12-31 15:30:45 +0000611 this.nameList = []
612This specifies a list, but the item type is unknown. Better use: >
613 this.nameList: list<string>
614The initialization isn't needed, the list is empty by default.
615 *E1330*
616Some types cannot be used, such as "void", "null" and "v:none".
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000617
618
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000619Defining an interface ~
620 *:interface* *:endinterface*
621An interface is defined between `:interface` and `:endinterface`. It may be
622prefixed with `:export`: >
623
624 interface InterfaceName
625 endinterface
626
627 export interface InterfaceName
628 endinterface
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000629< *E1344*
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700630An interface can declare object variables, just like in a class but without
631any initializer.
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000632 *E1345*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000633An interface can declare methods with `:def`, including the arguments and
634return type, but without the body and without `:enddef`. Example: >
635
636 interface HasSurface
637 this.size: number
638 def Surface(): number
639 endinterface
640
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000641An interface name must start with an uppercase letter. *E1343*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000642The "Has" prefix can be used to make it easier to guess this is an interface
643name, with a hint about what it provides.
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000644An interface can only be defined in a |Vim9| script file. *E1342*
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200645An interface cannot "implement" another interface but it can "extend" another
646interface. *E1381*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000647
648
Bram Moolenaar938ae282023-02-20 20:44:55 +0000649null object ~
650
Bram Moolenaardd60c362023-02-27 15:49:53 +0000651When a variable is declared to have the type of an object, but it is not
Bram Moolenaar938ae282023-02-20 20:44:55 +0000652initialized, the value is null. When trying to use this null object Vim often
653does not know what class was supposed to be used. Vim then cannot check if
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700654a variable name is correct and you will get an "Using a null object" error,
655even when the variable name is invalid. *E1360* *E1362* *E1363*
Bram Moolenaar938ae282023-02-20 20:44:55 +0000656
657
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000658Default constructor ~
Yegappan Lakshmanan413f8392023-09-28 22:46:37 +0200659 *default-constructor*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000660In case you define a class without a new() method, one will be automatically
661defined. This default constructor will have arguments for all the object
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700662variables, in the order they were specified. Thus if your class looks like: >
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000663
664 class AutoNew
665 this.name: string
666 this.age: number
667 this.gender: Gender
668 endclass
669
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700670Then the default constructor will be: >
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000671
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000672 def new(this.name = v:none, this.age = v:none, this.gender = v:none)
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000673 enddef
674
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000675The "= v:none" default values make the arguments optional. Thus you can also
676call `new()` without any arguments. No assignment will happen and the default
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700677value for the object variables will be used. This is a more useful example,
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000678with default values: >
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000679
680 class TextPosition
681 this.lnum: number = 1
682 this.col: number = 1
683 endclass
684
685If you want the constructor to have mandatory arguments, you need to write it
686yourself. For example, if for the AutoNew class above you insist on getting
687the name, you can define the constructor like this: >
688
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000689 def new(this.name, this.age = v:none, this.gender = v:none)
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000690 enddef
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000691< *E1328*
692Note that you cannot use another default value than "v:none" here. If you
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700693want to initialize the object variables, do it where they are declared. This
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000694way you only need to look in one place for the default values.
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000695
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700696All object variables will be used in the default constructor, also private
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000697access ones.
698
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700699If the class extends another one, the object variables of that class will come
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000700first.
701
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000702
703Multiple constructors ~
704
705Normally a class has just one new() constructor. In case you find that the
706constructor is often called with the same arguments you may want to simplify
707your code by putting those arguments into a second constructor method. For
708example, if you tend to use the color black a lot: >
709
710 def new(this.garment, this.color, this.size)
711 enddef
712 ...
713 var pants = new(Garment.pants, Color.black, "XL")
714 var shirt = new(Garment.shirt, Color.black, "XL")
715 var shoes = new(Garment.shoes, Color.black, "45")
716
717Instead of repeating the color every time you can add a constructor that
718includes it: >
719
720 def newBlack(this.garment, this.size)
721 this.color = Color.black
722 enddef
723 ...
724 var pants = newBlack(Garment.pants, "XL")
725 var shirt = newBlack(Garment.shirt, "XL")
726 var shoes = newBlack(Garment.shoes, "9.5")
727
728Note that the method name must start with "new". If there is no method called
729"new()" then the default constructor is added, even though there are other
730constructor methods.
731
732
733==============================================================================
734
Bram Moolenaarbe4e0162023-02-02 13:59:48 +00007357. Type definition *Vim9-type* *:type*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000736
737A type definition is giving a name to a type specification. For Example: >
738
739 :type ListOfStrings list<string>
740
741TODO: more explanation
742
743
744==============================================================================
745
Bram Moolenaarbe4e0162023-02-02 13:59:48 +00007468. Enum *Vim9-enum* *:enum* *:endenum*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000747
748An enum is a type that can have one of a list of values. Example: >
749
750 :enum Color
751 White
752 Red
753 Green
754 Blue
755 Black
756 :endenum
757
758TODO: more explanation
759
760
761==============================================================================
762
7639. Rationale
764
765Most of the choices for |Vim9| classes come from popular and recently
766developed languages, such as Java, TypeScript and Dart. The syntax has been
767made to fit with the way Vim script works, such as using `endclass` instead of
768using curly braces around the whole class.
769
770Some common constructs of object-oriented languages were chosen very long ago
771when this kind of programming was still new, and later found to be
772sub-optimal. By this time those constructs were widely used and changing them
773was not an option. In Vim we do have the freedom to make different choices,
774since classes are completely new. We can make the syntax simpler and more
775consistent than what "old" languages use. Without diverting too much, it
776should still mostly look like what you know from existing languages.
777
778Some recently developed languages add all kinds of fancy features that we
779don't need for Vim. But some have nice ideas that we do want to use.
780Thus we end up with a base of what is common in popular languages, dropping
781what looks like a bad idea, and adding some nice features that are easy to
782understand.
783
784The main rules we use to make decisions:
785- Keep it simple.
786- No surprises, mostly do what other languages are doing.
787- Avoid mistakes from the past.
788- Avoid the need for the script writer to consult the help to understand how
789 things work, most things should be obvious.
790- Keep it consistent.
791- Aim at an average size plugin, not at a huge project.
792
793
794Using new() for the constructor ~
795
796Many languages use the class name for the constructor method. A disadvantage
797is that quite often this is a long name. And when changing the class name all
798constructor methods need to be renamed. Not a big deal, but still a
799disadvantage.
800
801Other languages, such as TypeScript, use a specific name, such as
802"constructor()". That seems better. However, using "new" or "new()" to
803create a new object has no obvious relation with "constructor()".
804
805For |Vim9| script using the same method name for all constructors seemed like
806the right choice, and by calling it new() the relation between the caller and
807the method being called is obvious.
808
809
810No overloading of the constructor ~
811
812In Vim script, both legacy and |Vim9| script, there is no overloading of
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700813methods. That means it is not possible to use the same method name with
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000814different types of arguments. Therefore there also is only one new()
815constructor.
816
817With |Vim9| script it would be possible to support overloading, since
818arguments are typed. However, this gets complicated very quickly. Looking at
819a new() call one has to inspect the types of the arguments to know which of
820several new() methods is actually being called. And that can require
821inspecting quite a bit of code. For example, if one of the arguments is the
822return value of a method, you need to find that method to see what type it is
823returning.
824
825Instead, every constructor has to have a different name, starting with "new".
826That way multiple constructors with different arguments are possible, while it
827is very easy to see which constructor is being used. And the type of
828arguments can be properly checked.
829
830
831No overloading of methods ~
832
833Same reasoning as for the constructor: It is often not obvious what type
834arguments have, which would make it difficult to figure out what method is
835actually being called. Better just give the methods a different name, then
836type checking will make sure it works as you intended. This rules out
837polymorphism, which we don't really need anyway.
838
839
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000840Single inheritance and interfaces ~
841
842Some languages support multiple inheritance. Although that can be useful in
843some cases, it makes the rules of how a class works quite complicated.
844Instead, using interfaces to declare what is supported is much simpler. The
845very popular Java language does it this way, and it should be good enough for
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000846Vim. The "keep it simple" rule applies here.
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000847
848Explicitly declaring that a class supports an interface makes it easy to see
849what a class is intended for. It also makes it possible to do proper type
850checking. When an interface is changed any class that declares to implement
851it will be checked if that change was also changed. The mechanism to assume a
852class implements an interface just because the methods happen to match is
853brittle and leads to obscure problems, let's not do that.
854
855
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700856Using "this.variable" everywhere ~
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000857
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700858The object variables in various programming languages can often be accessed in
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000859different ways, depending on the location. Sometimes "this." has to be
860prepended to avoid ambiguity. They are usually declared without "this.".
861That is quite inconsistent and sometimes confusing.
862
863A very common issue is that in the constructor the arguments use the same name
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700864as the object variable. Then for these variables "this." needs to be prefixed
865in the body, while for other variables this is not needed and often omitted.
866This leads to a mix of variables with and without "this.", which is
867inconsistent.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000868
869For |Vim9| classes the "this." prefix is always used. Also for declaring the
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700870variables. Simple and consistent. When looking at the code inside a class
871it's also directly clear which variable references are object variables and
872which aren't.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000873
874
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700875Using class variables ~
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000876
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700877Using "static variable" to declare a class variable is very common, nothing
878new here. In |Vim9| script these can be accessed directly by their name.
879Very much like how a script-local variable can be used in a method. Since
880object variables are always accessed with "this." prepended, it's also quickly
881clear what kind of variable it is.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000882
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700883TypeScript prepends the class name before the class variable name, also inside
884the class. This has two problems: The class name can be rather long, taking
885up quite a bit of space, and when the class is renamed all these places need
886to be changed too.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000887
888
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700889Declaring object and class variables ~
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000890
891The main choice is whether to use "var" as with variable declarations.
892TypeScript does not use it: >
893 class Point {
894 x: number;
895 y = 0;
896 }
897
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700898Following that Vim object variables could be declared like this: >
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000899 class Point
900 this.x: number
901 this.y = 0
902 endclass
903
904Some users pointed out that this looks more like an assignment than a
905declaration. Adding "var" changes that: >
906 class Point
907 var this.x: number
908 var this.y = 0
909 endclass
910
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700911We also need to be able to declare class variables using the "static" keyword.
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000912There we can also choose to leave out "var": >
913 class Point
914 var this.x: number
915 static count = 0
916 endclass
917
918Or do use it, before "static": >
919 class Point
920 var this.x: number
921 var static count = 0
922 endclass
923
924Or after "static": >
925 class Point
926 var this.x: number
927 static var count = 0
928 endclass
929
930This is more in line with "static def Func()".
931
932There is no clear preference whether to use "var" or not. The two main
933reasons to leave it out are:
9341. TypeScript, Java and other popular languages do not use it.
9352. Less clutter.
936
937
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000938Using "ClassName.new()" to construct an object ~
939
940Many languages use the "new" operator to create an object, which is actually
941kind of strange, since the constructor is defined as a method with arguments,
942not a command. TypeScript also has the "new" keyword, but the method is
943called "constructor()", it is hard to see the relation between the two.
944
945In |Vim9| script the constructor method is called new(), and it is invoked as
946new(), simple and straightforward. Other languages use "new ClassName()",
947while there is no ClassName() method, it's a method by another name in the
948class called ClassName. Quite confusing.
949
950
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700951Default read access to object variables ~
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000952
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700953Some users will remark that the access rules for object variables are
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000954asymmetric. Well, that is intentional. Changing a value is a very different
955action than reading a value. The read operation has no side effects, it can
956be done any number of times without affecting the object. Changing the value
957can have many side effects, and even have a ripple effect, affecting other
958objects.
959
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700960When adding object variables one usually doesn't think much about this, just
961get the type right. And normally the values are set in the new() method.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000962Therefore defaulting to read access only "just works" in most cases. And when
963directly writing you get an error, which makes you wonder if you actually want
964to allow that. This helps writing code with fewer mistakes.
965
966
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700967Making object variables private with an underscore ~
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000968
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700969When an object variable is private, it can only be read and changed inside the
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000970class (and in sub-classes), then it cannot be used outside of the class.
971Prepending an underscore is a simple way to make that visible. Various
972programming languages have this as a recommendation.
973
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700974In case you change your mind and want to make the object variable accessible
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000975outside of the class, you will have to remove the underscore everywhere.
976Since the name only appears in the class (and sub-classes) they will be easy
977to find and change.
978
979The other way around is much harder: you can easily prepend an underscore to
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700980the object variable inside the class to make it private, but any usage
981elsewhere you will have to track down and change. You may have to make it a
982"set" method call. This reflects the real world problem that taking away
983access requires work to be done for all places where that access exists.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000984
985An alternative would have been using the "private" keyword, just like "public"
986changes the access in the other direction. Well, that's just to reduce the
987number of keywords.
988
989
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700990No protected object variables ~
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000991
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700992Some languages provide several ways to control access to object variables.
993The most known is "protected", and the meaning varies from language to
994language. Others are "shared", "private" and even "friend".
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000995
996These rules make life more difficult. That can be justified in projects where
997many people work on the same, complex code where it is easy to make mistakes.
998Especially when refactoring or other changes to the class model.
999
1000The Vim scripts are expected to be used in a plugin, with just one person or a
1001small team working on it. Complex rules then only make it more complicated,
Bram Moolenaar71badf92023-04-22 22:40:14 +01001002the extra safety provided by the rules isn't really needed. Let's just keep
1003it simple and not specify access details.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001004
1005
1006==============================================================================
1007
100810. To be done later
1009
1010Can a newSomething() constructor invoke another constructor? If yes, what are
1011the restrictions?
1012
1013Thoughts:
1014- Generics for a class: `class <Tkey, Tentry>`
1015- Generics for a function: `def <Tkey> GetLast(key: Tkey)`
1016- Mixins: not sure if that is useful, leave out for simplicity.
1017
1018Some things that look like good additions:
1019- For testing: Mock mechanism
1020
1021An important class to be provided is "Promise". Since Vim is single
1022threaded, connecting asynchronous operations is a natural way of allowing
1023plugins to do their work without blocking the user. It's a uniform way to
1024invoke callbacks and handle timeouts and errors.
1025
1026
1027 vim:tw=78:ts=8:noet:ft=help:norl: