blob: 066025469680513346f2da65a37174e469e5d62e [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
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 Lakshmananf3b68d42023-09-29 22:50:02 +0200550Object methods of the base class can be overruled. The number of arguments
551must be exactly the same. The method argument type can be a contra-variant
552type of the base class method argument type. The method return value type can
553be a covariant type of the base class method return value type. The method of
554the base class can be called by prefixing "super.".
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000555
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200556 *E1377*
557The access level of a method (public or private) in a child class should be
558the same as the super class.
559
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000560Other object methods of the base class are taken over by the child class.
561
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700562Class methods, including methods starting with "new", can be overruled, like
563with object methods. The method on the base class can be called by prefixing
564the name of the class (for class methods) or "super.".
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000565
566Unlike other languages, the constructor of the base class does not need to be
567invoked. In fact, it cannot be invoked. If some initialization from the base
568class also needs to be done in a child class, put it in an object method and
569call that method from every constructor().
570
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700571If the base class did not specify a new() method then one was automatically
572created. This method will not be taken over by the child class. The child
573class can define its own new() method, or, if there isn't one, a new() method
574will be added automatically.
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000575
576
577A class implementing an interface ~
Yegappan Lakshmanan2dede3d2023-09-27 19:02:01 +0200578 *implements* *E1346* *E1347* *E1389*
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000579A class can implement one or more interfaces. The "implements" keyword can
580only appear once *E1350* . Multiple interfaces can be specified, separated by
581commas. Each interface name can appear only once. *E1351*
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000582
583
584A class defining an interface ~
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000585 *specifies*
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700586A class can declare its interface, the object variables and methods, with a
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000587named interface. This avoids the need for separately specifying the
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000588interface, which is often done in many languages, especially Java.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000589
590
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000591Items in a class ~
Yegappan Lakshmanan2dede3d2023-09-27 19:02:01 +0200592 *E1318* *E1325* *E1388*
Bram Moolenaardd60c362023-02-27 15:49:53 +0000593Inside a class, in between `:class` and `:endclass`, these items can appear:
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700594- An object variable declaration: >
595 this._privateVariableName: memberType
596 this.readonlyVariableName: memberType
597 public this.readwriteVariableName: memberType
598- A class variable declaration: >
599 static _privateClassVariableName: memberType
600 static readonlyClassVariableName: memberType
601 static public readwriteClassVariableName: memberType
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000602- A constructor method: >
Bram Moolenaar938ae282023-02-20 20:44:55 +0000603 def new(arguments)
604 def newName(arguments)
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200605- A class method: >
606 static def SomeMethod(arguments)
607 static def _PrivateMethod(arguments)
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000608- An object method: >
Bram Moolenaar938ae282023-02-20 20:44:55 +0000609 def SomeMethod(arguments)
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200610 def _PrivateMethod(arguments)
611
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700612For the object variable the type must be specified. The best way is to do
613this explicitly with ": {type}". For simple types you can also use an
614initializer, such as "= 123", and Vim will see that the type is a number.
615Avoid doing this for more complex types and when the type will be incomplete.
616For example: >
Bram Moolenaarf1dcd142022-12-31 15:30:45 +0000617 this.nameList = []
618This specifies a list, but the item type is unknown. Better use: >
619 this.nameList: list<string>
620The initialization isn't needed, the list is empty by default.
621 *E1330*
622Some types cannot be used, such as "void", "null" and "v:none".
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000623
624
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000625Defining an interface ~
626 *:interface* *:endinterface*
627An interface is defined between `:interface` and `:endinterface`. It may be
628prefixed with `:export`: >
629
630 interface InterfaceName
631 endinterface
632
633 export interface InterfaceName
634 endinterface
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000635< *E1344*
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700636An interface can declare object variables, just like in a class but without
637any initializer.
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000638 *E1345*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000639An interface can declare methods with `:def`, including the arguments and
640return type, but without the body and without `:enddef`. Example: >
641
642 interface HasSurface
643 this.size: number
644 def Surface(): number
645 endinterface
646
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000647An interface name must start with an uppercase letter. *E1343*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000648The "Has" prefix can be used to make it easier to guess this is an interface
649name, with a hint about what it provides.
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000650An interface can only be defined in a |Vim9| script file. *E1342*
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200651An interface cannot "implement" another interface but it can "extend" another
652interface. *E1381*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000653
654
Bram Moolenaar938ae282023-02-20 20:44:55 +0000655null object ~
656
Bram Moolenaardd60c362023-02-27 15:49:53 +0000657When a variable is declared to have the type of an object, but it is not
Bram Moolenaar938ae282023-02-20 20:44:55 +0000658initialized, the value is null. When trying to use this null object Vim often
659does not know what class was supposed to be used. Vim then cannot check if
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700660a variable name is correct and you will get an "Using a null object" error,
661even when the variable name is invalid. *E1360* *E1362* *E1363*
Bram Moolenaar938ae282023-02-20 20:44:55 +0000662
663
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000664Default constructor ~
Yegappan Lakshmanan413f8392023-09-28 22:46:37 +0200665 *default-constructor*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000666In case you define a class without a new() method, one will be automatically
667defined. This default constructor will have arguments for all the object
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700668variables, in the order they were specified. Thus if your class looks like: >
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000669
670 class AutoNew
671 this.name: string
672 this.age: number
673 this.gender: Gender
674 endclass
675
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700676Then the default constructor will be: >
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000677
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000678 def new(this.name = v:none, this.age = v:none, this.gender = v:none)
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000679 enddef
680
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000681The "= v:none" default values make the arguments optional. Thus you can also
682call `new()` without any arguments. No assignment will happen and the default
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700683value for the object variables will be used. This is a more useful example,
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000684with default values: >
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000685
686 class TextPosition
687 this.lnum: number = 1
688 this.col: number = 1
689 endclass
690
691If you want the constructor to have mandatory arguments, you need to write it
692yourself. For example, if for the AutoNew class above you insist on getting
693the name, you can define the constructor like this: >
694
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000695 def new(this.name, this.age = v:none, this.gender = v:none)
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000696 enddef
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000697< *E1328*
698Note that you cannot use another default value than "v:none" here. If you
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700699want to initialize the object variables, do it where they are declared. This
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000700way you only need to look in one place for the default values.
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000701
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700702All object variables will be used in the default constructor, also private
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000703access ones.
704
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700705If the class extends another one, the object variables of that class will come
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000706first.
707
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000708
709Multiple constructors ~
710
711Normally a class has just one new() constructor. In case you find that the
712constructor is often called with the same arguments you may want to simplify
713your code by putting those arguments into a second constructor method. For
714example, if you tend to use the color black a lot: >
715
716 def new(this.garment, this.color, this.size)
717 enddef
718 ...
719 var pants = new(Garment.pants, Color.black, "XL")
720 var shirt = new(Garment.shirt, Color.black, "XL")
721 var shoes = new(Garment.shoes, Color.black, "45")
722
723Instead of repeating the color every time you can add a constructor that
724includes it: >
725
726 def newBlack(this.garment, this.size)
727 this.color = Color.black
728 enddef
729 ...
730 var pants = newBlack(Garment.pants, "XL")
731 var shirt = newBlack(Garment.shirt, "XL")
732 var shoes = newBlack(Garment.shoes, "9.5")
733
734Note that the method name must start with "new". If there is no method called
735"new()" then the default constructor is added, even though there are other
736constructor methods.
737
738
739==============================================================================
740
Bram Moolenaarbe4e0162023-02-02 13:59:48 +00007417. Type definition *Vim9-type* *:type*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000742
743A type definition is giving a name to a type specification. For Example: >
744
745 :type ListOfStrings list<string>
746
747TODO: more explanation
748
749
750==============================================================================
751
Bram Moolenaarbe4e0162023-02-02 13:59:48 +00007528. Enum *Vim9-enum* *:enum* *:endenum*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000753
754An enum is a type that can have one of a list of values. Example: >
755
756 :enum Color
757 White
758 Red
759 Green
760 Blue
761 Black
762 :endenum
763
764TODO: more explanation
765
766
767==============================================================================
768
7699. Rationale
770
771Most of the choices for |Vim9| classes come from popular and recently
772developed languages, such as Java, TypeScript and Dart. The syntax has been
773made to fit with the way Vim script works, such as using `endclass` instead of
774using curly braces around the whole class.
775
776Some common constructs of object-oriented languages were chosen very long ago
777when this kind of programming was still new, and later found to be
778sub-optimal. By this time those constructs were widely used and changing them
779was not an option. In Vim we do have the freedom to make different choices,
780since classes are completely new. We can make the syntax simpler and more
781consistent than what "old" languages use. Without diverting too much, it
782should still mostly look like what you know from existing languages.
783
784Some recently developed languages add all kinds of fancy features that we
785don't need for Vim. But some have nice ideas that we do want to use.
786Thus we end up with a base of what is common in popular languages, dropping
787what looks like a bad idea, and adding some nice features that are easy to
788understand.
789
790The main rules we use to make decisions:
791- Keep it simple.
792- No surprises, mostly do what other languages are doing.
793- Avoid mistakes from the past.
794- Avoid the need for the script writer to consult the help to understand how
795 things work, most things should be obvious.
796- Keep it consistent.
797- Aim at an average size plugin, not at a huge project.
798
799
800Using new() for the constructor ~
801
802Many languages use the class name for the constructor method. A disadvantage
803is that quite often this is a long name. And when changing the class name all
804constructor methods need to be renamed. Not a big deal, but still a
805disadvantage.
806
807Other languages, such as TypeScript, use a specific name, such as
808"constructor()". That seems better. However, using "new" or "new()" to
809create a new object has no obvious relation with "constructor()".
810
811For |Vim9| script using the same method name for all constructors seemed like
812the right choice, and by calling it new() the relation between the caller and
813the method being called is obvious.
814
815
816No overloading of the constructor ~
817
818In Vim script, both legacy and |Vim9| script, there is no overloading of
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700819methods. That means it is not possible to use the same method name with
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000820different types of arguments. Therefore there also is only one new()
821constructor.
822
823With |Vim9| script it would be possible to support overloading, since
824arguments are typed. However, this gets complicated very quickly. Looking at
825a new() call one has to inspect the types of the arguments to know which of
826several new() methods is actually being called. And that can require
827inspecting quite a bit of code. For example, if one of the arguments is the
828return value of a method, you need to find that method to see what type it is
829returning.
830
831Instead, every constructor has to have a different name, starting with "new".
832That way multiple constructors with different arguments are possible, while it
833is very easy to see which constructor is being used. And the type of
834arguments can be properly checked.
835
836
837No overloading of methods ~
838
839Same reasoning as for the constructor: It is often not obvious what type
840arguments have, which would make it difficult to figure out what method is
841actually being called. Better just give the methods a different name, then
842type checking will make sure it works as you intended. This rules out
843polymorphism, which we don't really need anyway.
844
845
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000846Single inheritance and interfaces ~
847
848Some languages support multiple inheritance. Although that can be useful in
849some cases, it makes the rules of how a class works quite complicated.
850Instead, using interfaces to declare what is supported is much simpler. The
851very popular Java language does it this way, and it should be good enough for
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000852Vim. The "keep it simple" rule applies here.
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000853
854Explicitly declaring that a class supports an interface makes it easy to see
855what a class is intended for. It also makes it possible to do proper type
856checking. When an interface is changed any class that declares to implement
857it will be checked if that change was also changed. The mechanism to assume a
858class implements an interface just because the methods happen to match is
859brittle and leads to obscure problems, let's not do that.
860
861
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700862Using "this.variable" everywhere ~
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000863
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700864The object variables in various programming languages can often be accessed in
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000865different ways, depending on the location. Sometimes "this." has to be
866prepended to avoid ambiguity. They are usually declared without "this.".
867That is quite inconsistent and sometimes confusing.
868
869A very common issue is that in the constructor the arguments use the same name
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700870as the object variable. Then for these variables "this." needs to be prefixed
871in the body, while for other variables this is not needed and often omitted.
872This leads to a mix of variables with and without "this.", which is
873inconsistent.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000874
875For |Vim9| classes the "this." prefix is always used. Also for declaring the
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700876variables. Simple and consistent. When looking at the code inside a class
877it's also directly clear which variable references are object variables and
878which aren't.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000879
880
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700881Using class variables ~
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000882
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700883Using "static variable" to declare a class variable is very common, nothing
884new here. In |Vim9| script these can be accessed directly by their name.
885Very much like how a script-local variable can be used in a method. Since
886object variables are always accessed with "this." prepended, it's also quickly
887clear what kind of variable it is.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000888
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700889TypeScript prepends the class name before the class variable name, also inside
890the class. This has two problems: The class name can be rather long, taking
891up quite a bit of space, and when the class is renamed all these places need
892to be changed too.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000893
894
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700895Declaring object and class variables ~
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000896
897The main choice is whether to use "var" as with variable declarations.
898TypeScript does not use it: >
899 class Point {
900 x: number;
901 y = 0;
902 }
903
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700904Following that Vim object variables could be declared like this: >
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000905 class Point
906 this.x: number
907 this.y = 0
908 endclass
909
910Some users pointed out that this looks more like an assignment than a
911declaration. Adding "var" changes that: >
912 class Point
913 var this.x: number
914 var this.y = 0
915 endclass
916
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700917We also need to be able to declare class variables using the "static" keyword.
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000918There we can also choose to leave out "var": >
919 class Point
920 var this.x: number
921 static count = 0
922 endclass
923
924Or do use it, before "static": >
925 class Point
926 var this.x: number
927 var static count = 0
928 endclass
929
930Or after "static": >
931 class Point
932 var this.x: number
933 static var count = 0
934 endclass
935
936This is more in line with "static def Func()".
937
938There is no clear preference whether to use "var" or not. The two main
939reasons to leave it out are:
9401. TypeScript, Java and other popular languages do not use it.
9412. Less clutter.
942
943
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000944Using "ClassName.new()" to construct an object ~
945
946Many languages use the "new" operator to create an object, which is actually
947kind of strange, since the constructor is defined as a method with arguments,
948not a command. TypeScript also has the "new" keyword, but the method is
949called "constructor()", it is hard to see the relation between the two.
950
951In |Vim9| script the constructor method is called new(), and it is invoked as
952new(), simple and straightforward. Other languages use "new ClassName()",
953while there is no ClassName() method, it's a method by another name in the
954class called ClassName. Quite confusing.
955
956
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700957Default read access to object variables ~
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000958
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700959Some users will remark that the access rules for object variables are
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000960asymmetric. Well, that is intentional. Changing a value is a very different
961action than reading a value. The read operation has no side effects, it can
962be done any number of times without affecting the object. Changing the value
963can have many side effects, and even have a ripple effect, affecting other
964objects.
965
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700966When adding object variables one usually doesn't think much about this, just
967get the type right. And normally the values are set in the new() method.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000968Therefore defaulting to read access only "just works" in most cases. And when
969directly writing you get an error, which makes you wonder if you actually want
970to allow that. This helps writing code with fewer mistakes.
971
972
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700973Making object variables private with an underscore ~
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000974
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700975When an object variable is private, it can only be read and changed inside the
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000976class (and in sub-classes), then it cannot be used outside of the class.
977Prepending an underscore is a simple way to make that visible. Various
978programming languages have this as a recommendation.
979
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700980In case you change your mind and want to make the object variable accessible
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000981outside of the class, you will have to remove the underscore everywhere.
982Since the name only appears in the class (and sub-classes) they will be easy
983to find and change.
984
985The other way around is much harder: you can easily prepend an underscore to
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700986the object variable inside the class to make it private, but any usage
987elsewhere you will have to track down and change. You may have to make it a
988"set" method call. This reflects the real world problem that taking away
989access requires work to be done for all places where that access exists.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000990
991An alternative would have been using the "private" keyword, just like "public"
992changes the access in the other direction. Well, that's just to reduce the
993number of keywords.
994
995
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700996No protected object variables ~
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000997
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700998Some languages provide several ways to control access to object variables.
999The most known is "protected", and the meaning varies from language to
1000language. Others are "shared", "private" and even "friend".
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001001
1002These rules make life more difficult. That can be justified in projects where
1003many people work on the same, complex code where it is easy to make mistakes.
1004Especially when refactoring or other changes to the class model.
1005
1006The Vim scripts are expected to be used in a plugin, with just one person or a
1007small team working on it. Complex rules then only make it more complicated,
Bram Moolenaar71badf92023-04-22 22:40:14 +01001008the extra safety provided by the rules isn't really needed. Let's just keep
1009it simple and not specify access details.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001010
1011
1012==============================================================================
1013
101410. To be done later
1015
1016Can a newSomething() constructor invoke another constructor? If yes, what are
1017the restrictions?
1018
1019Thoughts:
1020- Generics for a class: `class <Tkey, Tentry>`
1021- Generics for a function: `def <Tkey> GetLast(key: Tkey)`
1022- Mixins: not sure if that is useful, leave out for simplicity.
1023
1024Some things that look like good additions:
1025- For testing: Mock mechanism
1026
1027An important class to be provided is "Promise". Since Vim is single
1028threaded, connecting asynchronous operations is a natural way of allowing
1029plugins to do their work without blocking the user. It's a uniform way to
1030invoke callbacks and handle timeouts and errors.
1031
1032
1033 vim:tw=78:ts=8:noet:ft=help:norl: