blob: 00bdf369eb641b508c4a171280a85ea3d41bef72 [file] [log] [blame]
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02001*vim9class.txt* For Vim version 9.0. Last change: 2023 Sep 18
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00002
3
4 VIM REFERENCE MANUAL by Bram Moolenaar
5
6
Bram Moolenaarbe4e0162023-02-02 13:59:48 +00007NOTE - This is not finished yet, anything can still change! - NOTE
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00008
9
10Vim9 classes, objects, interfaces, types and enums.
11
121. Overview |Vim9-class-overview|
132. A simple class |Vim9-simple-class|
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200143. Class variables and methods |Vim9-class-member|
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000154. Using an abstract class |Vim9-abstract-class|
165. Using an interface |Vim9-using-interface|
176. More class details |Vim9-class|
187. Type definition |Vim9-type|
198. Enum |Vim9-enum|
Bram Moolenaarc1c365c2022-12-04 20:13:24 +000020
219. Rationale
2210. To be done later
23
24==============================================================================
25
261. Overview *Vim9-class-overview*
27
28The fancy term is "object-oriented programming". You can find lots of study
Bram Moolenaarbe4e0162023-02-02 13:59:48 +000029material on this subject. Here we document what |Vim9| script provides,
30assuming you know the basics already. Added are helpful hints about how to
31use this functionality effectively.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +000032
33The basic item is an object:
34- An object stores state. It contains one or more variables that can each
35 have a value.
Bram Moolenaarbe4e0162023-02-02 13:59:48 +000036- An object provides functions that use and manipulate its state. These
Bram Moolenaarc1c365c2022-12-04 20:13:24 +000037 functions are invoked "on the object", which is what sets it apart from the
38 traditional separation of data and code that manipulates the data.
39- An object has a well defined interface, with typed member variables and
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -070040 methods.
Bram Moolenaarbe4e0162023-02-02 13:59:48 +000041- Objects are created from a class and all objects have the same interface.
42 This does not change at runtime, it is not dynamic.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +000043
44An object can only be created by a class. A class provides:
45- A new() method, the constructor, which returns an object for the class.
46 This method is invoked on the class name: MyClass.new().
Bram Moolenaarbe4e0162023-02-02 13:59:48 +000047- State shared by all objects of the class: class variables (class members).
Bram Moolenaarc1c365c2022-12-04 20:13:24 +000048- A hierarchy of classes, with super-classes and sub-classes, inheritance.
49
50An interface is used to specify properties of an object:
51- An object can declare several interfaces that it implements.
52- Different objects implementing the same interface can be used the same way.
53
54The class hierarchy allows for single inheritance. Otherwise interfaces are
55to be used where needed.
56
57
58Class modeling ~
59
60You can model classes any way you like. Keep in mind what you are building,
61don't try to model the real world. This can be confusing, especially because
62teachers use real-world objects to explain class relations and you might think
63your model should therefore reflect the real world. It doesn't! The model
64should match your purpose.
65
Bram Moolenaarbe4e0162023-02-02 13:59:48 +000066Keep in mind that composition (an object contains other objects) is often
67better than inheritance (an object extends another object). Don't waste time
68trying to find the optimal class model. Or waste time discussing whether a
69square is a rectangle or that a rectangle is a square. It doesn't matter.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +000070
71
72==============================================================================
73
742. A simple class *Vim9-simple-class*
75
Bram Moolenaarbe4e0162023-02-02 13:59:48 +000076Let's start with a simple example: a class that stores a text position (see
77below for how to do this more efficiently): >
Bram Moolenaarc1c365c2022-12-04 20:13:24 +000078
79 class TextPosition
80 this.lnum: number
81 this.col: number
82
83 def new(lnum: number, col: number)
84 this.lnum = lnum
85 this.col = col
86 enddef
87
88 def SetLnum(lnum: number)
89 this.lnum = lnum
90 enddef
91
92 def SetCol(col: number)
93 this.col = col
94 enddef
95
96 def SetPosition(lnum: number, col: number)
97 this.lnum = lnum
98 this.col = col
99 enddef
100 endclass
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000101< *object* *Object*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000102You can create an object from this class with the new() method: >
103
104 var pos = TextPosition.new(1, 1)
105
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700106The object variables "lnum" and "col" can be accessed directly: >
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000107
108 echo $'The text position is ({pos.lnum}, {pos.col})'
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000109< *E1317* *E1327*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000110If you have been using other object-oriented languages you will notice that
111in Vim the object members are consistently referred to with the "this."
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000112prefix. This is different from languages like Java and TypeScript. The
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000113naming convention makes the object members easy to spot. Also, when a
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700114variable does not have the "this." prefix you know it is not an object
115variable.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000116
117
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700118Object variable write access ~
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000119
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700120Now try to change an object variable directly: >
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000121
122 pos.lnum = 9
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000123< *E1335*
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700124This will give you an error! That is because by default object variables can
125be read but not set. That's why the TextPosition class provides a method for
126it: >
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000127
128 pos.SetLnum(9)
129
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700130Allowing to read but not set an object variable is the most common and safest
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000131way. Most often there is no problem using a value, while setting a value may
132have side effects that need to be taken care of. In this case, the SetLnum()
133method could check if the line number is valid and either give an error or use
134the closest valid value.
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000135 *:public* *E1331*
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700136If you don't care about side effects and want to allow the object variable to
137be changed at any time, you can make it public: >
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000138
139 public this.lnum: number
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000140 public this.col: number
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000141
142Now you don't need the SetLnum(), SetCol() and SetPosition() methods, setting
143"pos.lnum" directly above will no longer give an error.
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200144 *E1326*
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700145If you try to set an object variable that doesn't exist you get an error: >
Bram Moolenaarf1dcd142022-12-31 15:30:45 +0000146 pos.other = 9
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200147< E1326: Member not found on object "TextPosition": other ~
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000148
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200149 *E1376*
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700150A object variable cannot be accessed using the class name.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000151
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700152Private variables ~
Yegappan Lakshmanan413f8392023-09-28 22:46:37 +0200153 *private-variable* *E1332* *E1333*
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700154On the other hand, if you do not want the object variables to be read directly,
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000155you can make them private. This is done by prefixing an underscore to the
156name: >
157
158 this._lnum: number
159 this._col number
160
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700161Now you need to provide methods to get the value of the private variables.
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000162These are commonly called getters. We recommend using a name that starts with
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000163"Get": >
164
165 def GetLnum(): number
166 return this._lnum
167 enddef
168
169 def GetCol() number
170 return this._col
171 enddef
172
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700173This example isn't very useful, the variables might as well have been public.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000174It does become useful if you check the value. For example, restrict the line
175number to the total number of lines: >
176
177 def GetLnum(): number
178 if this._lnum > this._lineCount
179 return this._lineCount
180 endif
181 return this._lnum
182 enddef
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200183<
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +0200184Private methods ~
Yegappan Lakshmanan413f8392023-09-28 22:46:37 +0200185 *private-method* *E1366*
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +0200186If you want object methods to be accessible only from other methods of the
187same class and not used from outside the class, then you can make them
188private. This is done by prefixing the method name with an underscore: >
189
190 class SomeClass
191 def _Foo(): number
192 return 10
193 enddef
194 def Bar(): number
195 return this._Foo()
196 enddef
197 endclass
198<
199Accessing a private method outside the class will result in an error (using
200the above class): >
201
202 var a = SomeClass.new()
203 a._Foo()
204<
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000205Simplifying the new() method ~
Yegappan Lakshmanan413f8392023-09-28 22:46:37 +0200206 *new()* *constructor*
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700207Many constructors take values for the object variables. Thus you very often
208see this pattern: >
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000209
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000210 class SomeClass
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000211 this.lnum: number
212 this.col: number
213
214 def new(lnum: number, col: number)
215 this.lnum = lnum
216 this.col = col
217 enddef
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000218 endclass
h-eastdb385522023-09-28 22:18:19 +0200219<
220 *E1390*
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700221Not only is this text you need to write, it also has the type of each
222variables twice. Since this is so common a shorter way to write new() is
223provided: >
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000224
225 def new(this.lnum, this.col)
226 enddef
227
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700228The semantics are easy to understand: Providing the object variable name,
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000229including "this.", as the argument to new() means the value provided in the
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700230new() call is assigned to that object variable. This mechanism comes from the
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000231Dart language.
232
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700233Putting together this way of using new() and making the variables public
234results in a much shorter class definition than what we started with: >
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000235
236 class TextPosition
237 public this.lnum: number
238 public this.col: number
239
240 def new(this.lnum, this.col)
241 enddef
242
243 def SetPosition(lnum: number, col: number)
244 this.lnum = lnum
245 this.col = col
246 enddef
247 endclass
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000248
249The sequence of constructing a new object is:
2501. Memory is allocated and cleared. All values are zero/false/empty.
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -07002512. For each declared object variable that has an initializer, the expression
252 is evaluated and assigned to the variable. This happens in the sequence
253 the variables are declared in the class.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00002543. Arguments in the new() method in the "this.name" form are assigned.
2554. The body of the new() method is executed.
256
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000257If the class extends a parent class, the same thing happens. In the second
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700258step the object variables of the parent class are initialized first. There is
259no need to call "super()" or "new()" on the parent.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000260
Yegappan Lakshmananb90e3bc2023-09-28 23:06:48 +0200261 *E1365*
Yegappan Lakshmanan6ac15442023-08-20 18:20:17 +0200262When defining the new() method the return type should not be specified. It
263always returns an object of the class.
264
Yegappan Lakshmananb90e3bc2023-09-28 23:06:48 +0200265 *E1386*
266When invoking an object method, the method name should be preceded by the
267object variable name. A object method cannot be invoked using the class
268name.
269
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000270==============================================================================
271
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02002723. Class Variables and Methods *Vim9-class-member*
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000273
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200274 *:static* *E1337* *E1338* *E1368*
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000275Class members are declared with "static". They are used by the name without a
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200276prefix in the class where they are defined: >
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000277
278 class OtherThing
279 this.size: number
280 static totalSize: number
281
282 def new(this.size)
283 totalSize += this.size
284 enddef
285 endclass
286< *E1340* *E1341*
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700287Since the name is used as-is, shadowing the name by a method argument name
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000288or local variable name is not allowed.
289
Yegappan Lakshmananb90e3bc2023-09-28 23:06:48 +0200290 *E1374* *E1375* *E1384* *E1385*
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200291To access a class member outside of the class where it is defined, the class
292name prefix must be used. A class member cannot be accessed using an object.
293
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000294Just like object members the access can be made private by using an underscore
295as the first character in the name, and it can be made public by prefixing
296"public": >
297
298 class OtherThing
299 static total: number # anybody can read, only class can write
300 static _sum: number # only class can read and write
301 public static result: number # anybody can read and write
302 endclass
303<
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200304 *class-method*
305Class methods are also declared with "static". They can use the class
306variables but they have no access to the object variables, they cannot use the
h_eastba77bbb2023-10-03 04:47:13 +0900307"this" keyword:
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200308>
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000309 class OtherThing
310 this.size: number
311 static totalSize: number
312
313 # Clear the total size and return the value it had before.
314 static def ClearTotalSize(): number
315 var prev = totalSize
316 totalSize = 0
317 return prev
318 enddef
319 endclass
320
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200321Inside the class the class method can be called by name directly, outside the
322class the class name must be prefixed: `OtherThing.ClearTotalSize()`. To use
323a super class method in a child class, the class name must be prefixed.
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000324
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +0200325Just like object methods the access can be made private by using an underscore
326as the first character in the method name: >
327
328 class OtherThing
329 static def _Foo()
330 echo "Foo"
331 enddef
332 def Bar()
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200333 _Foo()
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +0200334 enddef
335 endclass
Gianmaria Bajo4b9777a2023-08-29 22:26:30 +0200336<
337 *E1370*
338Note that constructors cannot be declared as "static", because they always
339are.
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +0200340
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200341To access the class methods and class variables of a super class in an
342extended class, the class name prefix should be used just as from anywhere
343outside of the defining class: >
344
345 vim9script
346 class Vehicle
347 static nextID: number = 1000
348 static def GetID(): number
349 nextID += 1
350 return nextID
351 enddef
352 endclass
353 class Car extends Vehicle
354 this.myID: number
355 def new()
356 this.myID = Vehicle.GetID()
357 enddef
358 endclass
359<
360Class variables and methods are not inherited by a child class. A child class
361can declare a static variable or a method with the same name as the one in the
362super class. Depending on the class where the member is used the
363corresponding class member will be used. The type of the class member in a
364child class can be different from that in the super class.
365
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000366==============================================================================
367
3684. Using an abstract class *Vim9-abstract-class*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000369
370An abstract class forms the base for at least one sub-class. In the class
371model one often finds that a few classes have the same properties that can be
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000372shared, but a class with these properties does not have enough state to create
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000373an object from. A sub-class must extend the abstract class and add the
374missing state and/or methods before it can be used to create objects for.
375
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000376For example, a Shape class could store a color and thickness. You cannot
377create a Shape object, it is missing the information about what kind of shape
378it is. The Shape class functions as the base for a Square and a Triangle
379class, for which objects can be created. Example: >
380
381 abstract class Shape
382 this.color = Color.Black
383 this.thickness = 10
384 endclass
385
386 class Square extends Shape
387 this.size: number
388
389 def new(this.size)
390 enddef
391 endclass
392
393 class Triangle extends Shape
394 this.base: number
395 this.height: number
396
397 def new(this.base, this.height)
398 enddef
399 endclass
400<
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000401An abstract class is defined the same way as a normal class, except that it
402does not have any new() method. *E1359*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000403
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200404 *abstract-method* *E1371* *E1372*
Yegappan Lakshmanan7bcd25c2023-09-08 19:27:51 +0200405An abstract method can be defined in an abstract class by using the "abstract"
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700406prefix when defining the method: >
Yegappan Lakshmanan7bcd25c2023-09-08 19:27:51 +0200407
408 abstract class Shape
409 abstract def Draw()
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200410 abstract static def SetColor()
Yegappan Lakshmanan7bcd25c2023-09-08 19:27:51 +0200411 endclass
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200412<
413 *E1373*
Yegappan Lakshmanan7bcd25c2023-09-08 19:27:51 +0200414A class extending the abstract class must implement all the abstract methods.
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200415The signature (arguments, argument types and return type) must be exactly the
Yegappan Lakshmanan26e8f7b2023-10-06 10:24:10 -0700416same. If the return type of a method is a class, then that class or one of
417its subclasses can be used in the extended method. Class methods in an
418abstract class can also be abstract methods.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000419
420==============================================================================
421
Bram Moolenaarbe4e0162023-02-02 13:59:48 +00004225. Using an interface *Vim9-using-interface*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000423
424The example above with Shape, Square and Triangle can be made more useful if
425we add a method to compute the surface of the object. For that we create the
426interface called HasSurface, which specifies one method Surface() that returns
427a number. This example extends the one above: >
428
429 abstract class Shape
430 this.color = Color.Black
431 this.thickness = 10
432 endclass
433
434 interface HasSurface
435 def Surface(): number
436 endinterface
437
438 class Square extends Shape implements HasSurface
439 this.size: number
440
441 def new(this.size)
442 enddef
443
444 def Surface(): number
445 return this.size * this.size
446 enddef
447 endclass
448
449 class Triangle extends Shape implements HasSurface
450 this.base: number
451 this.height: number
452
453 def new(this.base, this.height)
454 enddef
455
456 def Surface(): number
457 return this.base * this.height / 2
458 enddef
459 endclass
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200460<
461 *E1348* *E1349* *E1367* *E1382* *E1383*
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000462If a class declares to implement an interface, all the items specified in the
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200463interface must appear in the class, with the same types.
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000464
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000465The interface name can be used as a type: >
466
467 var shapes: list<HasSurface> = [
468 Square.new(12),
469 Triangle.new(8, 15),
470 ]
471 for shape in shapes
472 echo $'the surface is {shape.Surface()}'
473 endfor
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200474<
Yegappan Lakshmanan2dede3d2023-09-27 19:02:01 +0200475 *E1378* *E1379* *E1380* *E1387*
476An interface can contain only object methods and read-only object variables.
477An interface cannot contain read-write and private object variables, private
478object methods, class variables and class methods.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000479
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200480An interface can extend another interface using "extends". The sub-interface
481inherits all the instance variables and methods from the super interface.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000482
483==============================================================================
484
Bram Moolenaarbe4e0162023-02-02 13:59:48 +00004856. More class details *Vim9-class* *Class* *class*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000486
487Defining a class ~
488 *:class* *:endclass* *:abstract*
489A class is defined between `:class` and `:endclass`. The whole class is
490defined in one script file. It is not possible to add to a class later.
491
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000492A class can only be defined in a |Vim9| script file. *E1316*
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000493A class cannot be defined inside a function.
494
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000495It is possible to define more than one class in a script file. Although it
496usually is better to export only one main class. It can be useful to define
497types, enums and helper classes though.
498
499The `:abstract` keyword may be prefixed and `:export` may be used. That gives
500these variants: >
501
502 class ClassName
503 endclass
504
505 export class ClassName
506 endclass
507
508 abstract class ClassName
509 endclass
510
511 export abstract class ClassName
512 endclass
513<
514 *E1314*
515The class name should be CamelCased. It must start with an uppercase letter.
516That avoids clashing with builtin types.
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000517 *E1315*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000518After the class name these optional items can be used. Each can appear only
519once. They can appear in any order, although this order is recommended: >
520 extends ClassName
521 implements InterfaceName, OtherInterface
522 specifies SomeInterface
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200523< *E1355* *E1369*
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700524Each variable and method name can be used only once. It is not possible to
525define a method with the same name and different type of arguments. It is not
526possible to use a public and private member variable with the same name. A
527object variable name used in a super class cannot be reused in a child class.
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000528
529
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700530Object Variable Initialization ~
531If the type of a variable is not explicitly specified in a class, then it is
532set to "any" during class definition. When an object is instantiated from the
533class, then the type of the variable is set.
Yegappan Lakshmanan618e47d2023-08-22 21:29:28 +0200534
Yegappan Lakshmananf3b68d42023-09-29 22:50:02 +0200535The following reserved keyword names cannot be used as an object or class
536variable name: "super", "this", "true", "false", "null", "null_blob",
537"null_dict", "null_function", "null_list", "null_partial", "null_string",
538"null_channel" and "null_job".
Yegappan Lakshmanan618e47d2023-08-22 21:29:28 +0200539
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000540Extending a class ~
541 *extends*
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000542A class can extend one other class. *E1352* *E1353* *E1354*
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000543The basic idea is to build on top of an existing class, add properties to it.
544
545The extended class is called the "base class" or "super class". The new class
546is called the "child class".
547
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700548Object variables from the base class are all taken over by the child class. It
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000549is not possible to override them (unlike some other languages).
550
551 *E1356* *E1357* *E1358*
Yegappan Lakshmananb32064f2023-10-02 21:43:58 +0200552Object methods of the base class can be overruled. The signature (arguments,
Yegappan Lakshmanan26e8f7b2023-10-06 10:24:10 -0700553argument types and return type) must be exactly the same. If the return type
554of a method is a class, then that class or one of its subclasses can be used
555in the extended method. The method of the base class can be called by
556prefixing "super.".
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000557
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200558 *E1377*
559The access level of a method (public or private) in a child class should be
560the same as the super class.
561
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000562Other object methods of the base class are taken over by the child class.
563
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700564Class methods, including methods starting with "new", can be overruled, like
565with object methods. The method on the base class can be called by prefixing
566the name of the class (for class methods) or "super.".
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000567
568Unlike other languages, the constructor of the base class does not need to be
569invoked. In fact, it cannot be invoked. If some initialization from the base
570class also needs to be done in a child class, put it in an object method and
571call that method from every constructor().
572
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700573If the base class did not specify a new() method then one was automatically
574created. This method will not be taken over by the child class. The child
575class can define its own new() method, or, if there isn't one, a new() method
576will be added automatically.
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000577
578
579A class implementing an interface ~
Yegappan Lakshmanan2dede3d2023-09-27 19:02:01 +0200580 *implements* *E1346* *E1347* *E1389*
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000581A class can implement one or more interfaces. The "implements" keyword can
582only appear once *E1350* . Multiple interfaces can be specified, separated by
583commas. Each interface name can appear only once. *E1351*
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000584
585
586A class defining an interface ~
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000587 *specifies*
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700588A class can declare its interface, the object variables and methods, with a
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000589named interface. This avoids the need for separately specifying the
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000590interface, which is often done in many languages, especially Java.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000591
592
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000593Items in a class ~
Yegappan Lakshmanan2dede3d2023-09-27 19:02:01 +0200594 *E1318* *E1325* *E1388*
Bram Moolenaardd60c362023-02-27 15:49:53 +0000595Inside a class, in between `:class` and `:endclass`, these items can appear:
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700596- An object variable declaration: >
597 this._privateVariableName: memberType
598 this.readonlyVariableName: memberType
599 public this.readwriteVariableName: memberType
600- A class variable declaration: >
601 static _privateClassVariableName: memberType
602 static readonlyClassVariableName: memberType
603 static public readwriteClassVariableName: memberType
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000604- A constructor method: >
Bram Moolenaar938ae282023-02-20 20:44:55 +0000605 def new(arguments)
606 def newName(arguments)
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200607- A class method: >
608 static def SomeMethod(arguments)
609 static def _PrivateMethod(arguments)
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000610- An object method: >
Bram Moolenaar938ae282023-02-20 20:44:55 +0000611 def SomeMethod(arguments)
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200612 def _PrivateMethod(arguments)
613
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700614For the object variable the type must be specified. The best way is to do
615this explicitly with ": {type}". For simple types you can also use an
616initializer, such as "= 123", and Vim will see that the type is a number.
617Avoid doing this for more complex types and when the type will be incomplete.
618For example: >
Bram Moolenaarf1dcd142022-12-31 15:30:45 +0000619 this.nameList = []
620This specifies a list, but the item type is unknown. Better use: >
621 this.nameList: list<string>
622The initialization isn't needed, the list is empty by default.
623 *E1330*
624Some types cannot be used, such as "void", "null" and "v:none".
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000625
626
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000627Defining an interface ~
628 *:interface* *:endinterface*
629An interface is defined between `:interface` and `:endinterface`. It may be
630prefixed with `:export`: >
631
632 interface InterfaceName
633 endinterface
634
635 export interface InterfaceName
636 endinterface
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000637< *E1344*
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700638An interface can declare object variables, just like in a class but without
639any initializer.
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000640 *E1345*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000641An interface can declare methods with `:def`, including the arguments and
642return type, but without the body and without `:enddef`. Example: >
643
644 interface HasSurface
645 this.size: number
646 def Surface(): number
647 endinterface
648
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000649An interface name must start with an uppercase letter. *E1343*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000650The "Has" prefix can be used to make it easier to guess this is an interface
651name, with a hint about what it provides.
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000652An interface can only be defined in a |Vim9| script file. *E1342*
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200653An interface cannot "implement" another interface but it can "extend" another
654interface. *E1381*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000655
656
Bram Moolenaar938ae282023-02-20 20:44:55 +0000657null object ~
658
Bram Moolenaardd60c362023-02-27 15:49:53 +0000659When a variable is declared to have the type of an object, but it is not
Bram Moolenaar938ae282023-02-20 20:44:55 +0000660initialized, the value is null. When trying to use this null object Vim often
661does not know what class was supposed to be used. Vim then cannot check if
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700662a variable name is correct and you will get an "Using a null object" error,
h_eastba77bbb2023-10-03 04:47:13 +0900663even when the variable name is invalid. *E1360* *E1362*
Bram Moolenaar938ae282023-02-20 20:44:55 +0000664
665
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000666Default constructor ~
Yegappan Lakshmanan413f8392023-09-28 22:46:37 +0200667 *default-constructor*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000668In case you define a class without a new() method, one will be automatically
669defined. This default constructor will have arguments for all the object
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700670variables, in the order they were specified. Thus if your class looks like: >
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000671
672 class AutoNew
673 this.name: string
674 this.age: number
675 this.gender: Gender
676 endclass
677
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700678Then the default constructor will be: >
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000679
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000680 def new(this.name = v:none, this.age = v:none, this.gender = v:none)
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000681 enddef
682
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000683The "= v:none" default values make the arguments optional. Thus you can also
684call `new()` without any arguments. No assignment will happen and the default
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700685value for the object variables will be used. This is a more useful example,
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000686with default values: >
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000687
688 class TextPosition
689 this.lnum: number = 1
690 this.col: number = 1
691 endclass
692
693If you want the constructor to have mandatory arguments, you need to write it
694yourself. For example, if for the AutoNew class above you insist on getting
695the name, you can define the constructor like this: >
696
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000697 def new(this.name, this.age = v:none, this.gender = v:none)
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000698 enddef
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000699< *E1328*
700Note that you cannot use another default value than "v:none" here. If you
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700701want to initialize the object variables, do it where they are declared. This
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000702way you only need to look in one place for the default values.
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000703
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700704All object variables will be used in the default constructor, also private
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000705access ones.
706
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700707If the class extends another one, the object variables of that class will come
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000708first.
709
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000710
711Multiple constructors ~
712
713Normally a class has just one new() constructor. In case you find that the
714constructor is often called with the same arguments you may want to simplify
715your code by putting those arguments into a second constructor method. For
716example, if you tend to use the color black a lot: >
717
718 def new(this.garment, this.color, this.size)
719 enddef
720 ...
721 var pants = new(Garment.pants, Color.black, "XL")
722 var shirt = new(Garment.shirt, Color.black, "XL")
723 var shoes = new(Garment.shoes, Color.black, "45")
724
725Instead of repeating the color every time you can add a constructor that
726includes it: >
727
728 def newBlack(this.garment, this.size)
729 this.color = Color.black
730 enddef
731 ...
732 var pants = newBlack(Garment.pants, "XL")
733 var shirt = newBlack(Garment.shirt, "XL")
734 var shoes = newBlack(Garment.shoes, "9.5")
735
736Note that the method name must start with "new". If there is no method called
737"new()" then the default constructor is added, even though there are other
738constructor methods.
739
740
741==============================================================================
742
Bram Moolenaarbe4e0162023-02-02 13:59:48 +00007437. Type definition *Vim9-type* *:type*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000744
Yegappan Lakshmanan26e8f7b2023-10-06 10:24:10 -0700745{not implemented yet}
746
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000747A type definition is giving a name to a type specification. For Example: >
748
749 :type ListOfStrings list<string>
750
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000751
752==============================================================================
753
Bram Moolenaarbe4e0162023-02-02 13:59:48 +00007548. Enum *Vim9-enum* *:enum* *:endenum*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000755
Yegappan Lakshmanan26e8f7b2023-10-06 10:24:10 -0700756{not implemented yet}
757
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000758An enum is a type that can have one of a list of values. Example: >
759
760 :enum Color
761 White
762 Red
763 Green
764 Blue
765 Black
766 :endenum
767
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000768
769==============================================================================
770
7719. Rationale
772
773Most of the choices for |Vim9| classes come from popular and recently
774developed languages, such as Java, TypeScript and Dart. The syntax has been
775made to fit with the way Vim script works, such as using `endclass` instead of
776using curly braces around the whole class.
777
778Some common constructs of object-oriented languages were chosen very long ago
779when this kind of programming was still new, and later found to be
780sub-optimal. By this time those constructs were widely used and changing them
781was not an option. In Vim we do have the freedom to make different choices,
782since classes are completely new. We can make the syntax simpler and more
783consistent than what "old" languages use. Without diverting too much, it
784should still mostly look like what you know from existing languages.
785
786Some recently developed languages add all kinds of fancy features that we
787don't need for Vim. But some have nice ideas that we do want to use.
788Thus we end up with a base of what is common in popular languages, dropping
789what looks like a bad idea, and adding some nice features that are easy to
790understand.
791
792The main rules we use to make decisions:
793- Keep it simple.
794- No surprises, mostly do what other languages are doing.
795- Avoid mistakes from the past.
796- Avoid the need for the script writer to consult the help to understand how
797 things work, most things should be obvious.
798- Keep it consistent.
799- Aim at an average size plugin, not at a huge project.
800
801
802Using new() for the constructor ~
803
804Many languages use the class name for the constructor method. A disadvantage
805is that quite often this is a long name. And when changing the class name all
806constructor methods need to be renamed. Not a big deal, but still a
807disadvantage.
808
809Other languages, such as TypeScript, use a specific name, such as
810"constructor()". That seems better. However, using "new" or "new()" to
811create a new object has no obvious relation with "constructor()".
812
813For |Vim9| script using the same method name for all constructors seemed like
814the right choice, and by calling it new() the relation between the caller and
815the method being called is obvious.
816
817
818No overloading of the constructor ~
819
820In Vim script, both legacy and |Vim9| script, there is no overloading of
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700821methods. That means it is not possible to use the same method name with
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000822different types of arguments. Therefore there also is only one new()
823constructor.
824
825With |Vim9| script it would be possible to support overloading, since
826arguments are typed. However, this gets complicated very quickly. Looking at
827a new() call one has to inspect the types of the arguments to know which of
828several new() methods is actually being called. And that can require
829inspecting quite a bit of code. For example, if one of the arguments is the
830return value of a method, you need to find that method to see what type it is
831returning.
832
833Instead, every constructor has to have a different name, starting with "new".
834That way multiple constructors with different arguments are possible, while it
835is very easy to see which constructor is being used. And the type of
836arguments can be properly checked.
837
838
839No overloading of methods ~
840
841Same reasoning as for the constructor: It is often not obvious what type
842arguments have, which would make it difficult to figure out what method is
843actually being called. Better just give the methods a different name, then
844type checking will make sure it works as you intended. This rules out
845polymorphism, which we don't really need anyway.
846
847
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000848Single inheritance and interfaces ~
849
850Some languages support multiple inheritance. Although that can be useful in
851some cases, it makes the rules of how a class works quite complicated.
852Instead, using interfaces to declare what is supported is much simpler. The
853very popular Java language does it this way, and it should be good enough for
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000854Vim. The "keep it simple" rule applies here.
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000855
856Explicitly declaring that a class supports an interface makes it easy to see
857what a class is intended for. It also makes it possible to do proper type
858checking. When an interface is changed any class that declares to implement
859it will be checked if that change was also changed. The mechanism to assume a
860class implements an interface just because the methods happen to match is
861brittle and leads to obscure problems, let's not do that.
862
863
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700864Using "this.variable" everywhere ~
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000865
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700866The object variables in various programming languages can often be accessed in
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000867different ways, depending on the location. Sometimes "this." has to be
868prepended to avoid ambiguity. They are usually declared without "this.".
869That is quite inconsistent and sometimes confusing.
870
871A very common issue is that in the constructor the arguments use the same name
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700872as the object variable. Then for these variables "this." needs to be prefixed
873in the body, while for other variables this is not needed and often omitted.
874This leads to a mix of variables with and without "this.", which is
875inconsistent.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000876
877For |Vim9| classes the "this." prefix is always used. Also for declaring the
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700878variables. Simple and consistent. When looking at the code inside a class
879it's also directly clear which variable references are object variables and
880which aren't.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000881
882
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700883Using class variables ~
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000884
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700885Using "static variable" to declare a class variable is very common, nothing
886new here. In |Vim9| script these can be accessed directly by their name.
887Very much like how a script-local variable can be used in a method. Since
888object variables are always accessed with "this." prepended, it's also quickly
889clear what kind of variable it is.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000890
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700891TypeScript prepends the class name before the class variable name, also inside
892the class. This has two problems: The class name can be rather long, taking
893up quite a bit of space, and when the class is renamed all these places need
894to be changed too.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000895
896
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700897Declaring object and class variables ~
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000898
899The main choice is whether to use "var" as with variable declarations.
900TypeScript does not use it: >
901 class Point {
902 x: number;
903 y = 0;
904 }
905
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700906Following that Vim object variables could be declared like this: >
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000907 class Point
908 this.x: number
909 this.y = 0
910 endclass
911
912Some users pointed out that this looks more like an assignment than a
913declaration. Adding "var" changes that: >
914 class Point
915 var this.x: number
916 var this.y = 0
917 endclass
918
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700919We also need to be able to declare class variables using the "static" keyword.
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000920There we can also choose to leave out "var": >
921 class Point
922 var this.x: number
923 static count = 0
924 endclass
925
926Or do use it, before "static": >
927 class Point
928 var this.x: number
929 var static count = 0
930 endclass
931
932Or after "static": >
933 class Point
934 var this.x: number
935 static var count = 0
936 endclass
937
938This is more in line with "static def Func()".
939
940There is no clear preference whether to use "var" or not. The two main
941reasons to leave it out are:
9421. TypeScript, Java and other popular languages do not use it.
9432. Less clutter.
944
945
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000946Using "ClassName.new()" to construct an object ~
947
948Many languages use the "new" operator to create an object, which is actually
949kind of strange, since the constructor is defined as a method with arguments,
950not a command. TypeScript also has the "new" keyword, but the method is
951called "constructor()", it is hard to see the relation between the two.
952
953In |Vim9| script the constructor method is called new(), and it is invoked as
954new(), simple and straightforward. Other languages use "new ClassName()",
955while there is no ClassName() method, it's a method by another name in the
956class called ClassName. Quite confusing.
957
958
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700959Default read access to object variables ~
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000960
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700961Some users will remark that the access rules for object variables are
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000962asymmetric. Well, that is intentional. Changing a value is a very different
963action than reading a value. The read operation has no side effects, it can
964be done any number of times without affecting the object. Changing the value
965can have many side effects, and even have a ripple effect, affecting other
966objects.
967
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700968When adding object variables one usually doesn't think much about this, just
969get the type right. And normally the values are set in the new() method.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000970Therefore defaulting to read access only "just works" in most cases. And when
971directly writing you get an error, which makes you wonder if you actually want
972to allow that. This helps writing code with fewer mistakes.
973
974
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700975Making object variables private with an underscore ~
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000976
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700977When an object variable is private, it can only be read and changed inside the
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000978class (and in sub-classes), then it cannot be used outside of the class.
979Prepending an underscore is a simple way to make that visible. Various
980programming languages have this as a recommendation.
981
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700982In case you change your mind and want to make the object variable accessible
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000983outside of the class, you will have to remove the underscore everywhere.
984Since the name only appears in the class (and sub-classes) they will be easy
985to find and change.
986
987The other way around is much harder: you can easily prepend an underscore to
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700988the object variable inside the class to make it private, but any usage
989elsewhere you will have to track down and change. You may have to make it a
990"set" method call. This reflects the real world problem that taking away
991access requires work to be done for all places where that access exists.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000992
993An alternative would have been using the "private" keyword, just like "public"
994changes the access in the other direction. Well, that's just to reduce the
995number of keywords.
996
997
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700998No protected object variables ~
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000999
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -07001000Some languages provide several ways to control access to object variables.
1001The most known is "protected", and the meaning varies from language to
1002language. Others are "shared", "private" and even "friend".
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001003
1004These rules make life more difficult. That can be justified in projects where
1005many people work on the same, complex code where it is easy to make mistakes.
1006Especially when refactoring or other changes to the class model.
1007
1008The Vim scripts are expected to be used in a plugin, with just one person or a
1009small team working on it. Complex rules then only make it more complicated,
Bram Moolenaar71badf92023-04-22 22:40:14 +01001010the extra safety provided by the rules isn't really needed. Let's just keep
1011it simple and not specify access details.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00001012
1013
1014==============================================================================
1015
101610. To be done later
1017
1018Can a newSomething() constructor invoke another constructor? If yes, what are
1019the restrictions?
1020
1021Thoughts:
1022- Generics for a class: `class <Tkey, Tentry>`
1023- Generics for a function: `def <Tkey> GetLast(key: Tkey)`
1024- Mixins: not sure if that is useful, leave out for simplicity.
1025
1026Some things that look like good additions:
1027- For testing: Mock mechanism
1028
1029An important class to be provided is "Promise". Since Vim is single
1030threaded, connecting asynchronous operations is a natural way of allowing
1031plugins to do their work without blocking the user. It's a uniform way to
1032invoke callbacks and handle timeouts and errors.
1033
1034
1035 vim:tw=78:ts=8:noet:ft=help:norl: