blob: 7b9f8b4a6c7f1541934678dbead3ccd6f74bae42 [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 ~
Bram Moolenaarf1dcd142022-12-31 15:30:45 +0000153 *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 Lakshmanan00cd1822023-09-18 19:56:49 +0200185 *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 ~
206
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 Lakshmanan00cd1822023-09-18 19:56:49 +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
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000265==============================================================================
266
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02002673. Class Variables and Methods *Vim9-class-member*
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000268
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200269 *:static* *E1337* *E1338* *E1368*
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000270Class members are declared with "static". They are used by the name without a
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200271prefix in the class where they are defined: >
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000272
273 class OtherThing
274 this.size: number
275 static totalSize: number
276
277 def new(this.size)
278 totalSize += this.size
279 enddef
280 endclass
281< *E1340* *E1341*
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700282Since the name is used as-is, shadowing the name by a method argument name
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000283or local variable name is not allowed.
284
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200285 *E1374* *E1375*
286To access a class member outside of the class where it is defined, the class
287name prefix must be used. A class member cannot be accessed using an object.
288
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000289Just like object members the access can be made private by using an underscore
290as the first character in the name, and it can be made public by prefixing
291"public": >
292
293 class OtherThing
294 static total: number # anybody can read, only class can write
295 static _sum: number # only class can read and write
296 public static result: number # anybody can read and write
297 endclass
298<
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200299 *class-method*
300Class methods are also declared with "static". They can use the class
301variables but they have no access to the object variables, they cannot use the
302"this" keyword.
303>
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000304 class OtherThing
305 this.size: number
306 static totalSize: number
307
308 # Clear the total size and return the value it had before.
309 static def ClearTotalSize(): number
310 var prev = totalSize
311 totalSize = 0
312 return prev
313 enddef
314 endclass
315
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200316Inside the class the class method can be called by name directly, outside the
317class the class name must be prefixed: `OtherThing.ClearTotalSize()`. To use
318a super class method in a child class, the class name must be prefixed.
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000319
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +0200320Just like object methods the access can be made private by using an underscore
321as the first character in the method name: >
322
323 class OtherThing
324 static def _Foo()
325 echo "Foo"
326 enddef
327 def Bar()
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200328 _Foo()
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +0200329 enddef
330 endclass
Gianmaria Bajo4b9777a2023-08-29 22:26:30 +0200331<
332 *E1370*
333Note that constructors cannot be declared as "static", because they always
334are.
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +0200335
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200336To access the class methods and class variables of a super class in an
337extended class, the class name prefix should be used just as from anywhere
338outside of the defining class: >
339
340 vim9script
341 class Vehicle
342 static nextID: number = 1000
343 static def GetID(): number
344 nextID += 1
345 return nextID
346 enddef
347 endclass
348 class Car extends Vehicle
349 this.myID: number
350 def new()
351 this.myID = Vehicle.GetID()
352 enddef
353 endclass
354<
355Class variables and methods are not inherited by a child class. A child class
356can declare a static variable or a method with the same name as the one in the
357super class. Depending on the class where the member is used the
358corresponding class member will be used. The type of the class member in a
359child class can be different from that in the super class.
360
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000361==============================================================================
362
3634. Using an abstract class *Vim9-abstract-class*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000364
365An abstract class forms the base for at least one sub-class. In the class
366model one often finds that a few classes have the same properties that can be
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000367shared, but a class with these properties does not have enough state to create
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000368an object from. A sub-class must extend the abstract class and add the
369missing state and/or methods before it can be used to create objects for.
370
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000371For example, a Shape class could store a color and thickness. You cannot
372create a Shape object, it is missing the information about what kind of shape
373it is. The Shape class functions as the base for a Square and a Triangle
374class, for which objects can be created. Example: >
375
376 abstract class Shape
377 this.color = Color.Black
378 this.thickness = 10
379 endclass
380
381 class Square extends Shape
382 this.size: number
383
384 def new(this.size)
385 enddef
386 endclass
387
388 class Triangle extends Shape
389 this.base: number
390 this.height: number
391
392 def new(this.base, this.height)
393 enddef
394 endclass
395<
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000396An abstract class is defined the same way as a normal class, except that it
397does not have any new() method. *E1359*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000398
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200399 *abstract-method* *E1371* *E1372*
Yegappan Lakshmanan7bcd25c2023-09-08 19:27:51 +0200400An abstract method can be defined in an abstract class by using the "abstract"
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700401prefix when defining the method: >
Yegappan Lakshmanan7bcd25c2023-09-08 19:27:51 +0200402
403 abstract class Shape
404 abstract def Draw()
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200405 abstract static def SetColor()
Yegappan Lakshmanan7bcd25c2023-09-08 19:27:51 +0200406 endclass
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200407<
408 *E1373*
Yegappan Lakshmanan7bcd25c2023-09-08 19:27:51 +0200409A class extending the abstract class must implement all the abstract methods.
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200410The signature (arguments, argument types and return type) must be exactly the
411same. Class methods in an abstract class can also be abstract methods.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000412
413==============================================================================
414
Bram Moolenaarbe4e0162023-02-02 13:59:48 +00004155. Using an interface *Vim9-using-interface*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000416
417The example above with Shape, Square and Triangle can be made more useful if
418we add a method to compute the surface of the object. For that we create the
419interface called HasSurface, which specifies one method Surface() that returns
420a number. This example extends the one above: >
421
422 abstract class Shape
423 this.color = Color.Black
424 this.thickness = 10
425 endclass
426
427 interface HasSurface
428 def Surface(): number
429 endinterface
430
431 class Square extends Shape implements HasSurface
432 this.size: number
433
434 def new(this.size)
435 enddef
436
437 def Surface(): number
438 return this.size * this.size
439 enddef
440 endclass
441
442 class Triangle extends Shape implements HasSurface
443 this.base: number
444 this.height: number
445
446 def new(this.base, this.height)
447 enddef
448
449 def Surface(): number
450 return this.base * this.height / 2
451 enddef
452 endclass
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200453<
454 *E1348* *E1349* *E1367* *E1382* *E1383*
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000455If a class declares to implement an interface, all the items specified in the
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200456interface must appear in the class, with the same types.
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000457
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000458The interface name can be used as a type: >
459
460 var shapes: list<HasSurface> = [
461 Square.new(12),
462 Triangle.new(8, 15),
463 ]
464 for shape in shapes
465 echo $'the surface is {shape.Surface()}'
466 endfor
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200467<
Yegappan Lakshmanan2dede3d2023-09-27 19:02:01 +0200468 *E1378* *E1379* *E1380* *E1387*
469An interface can contain only object methods and read-only object variables.
470An interface cannot contain read-write and private object variables, private
471object methods, class variables and class methods.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000472
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200473An interface can extend another interface using "extends". The sub-interface
474inherits all the instance variables and methods from the super interface.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000475
476==============================================================================
477
Bram Moolenaarbe4e0162023-02-02 13:59:48 +00004786. More class details *Vim9-class* *Class* *class*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000479
480Defining a class ~
481 *:class* *:endclass* *:abstract*
482A class is defined between `:class` and `:endclass`. The whole class is
483defined in one script file. It is not possible to add to a class later.
484
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000485A class can only be defined in a |Vim9| script file. *E1316*
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000486A class cannot be defined inside a function.
487
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000488It is possible to define more than one class in a script file. Although it
489usually is better to export only one main class. It can be useful to define
490types, enums and helper classes though.
491
492The `:abstract` keyword may be prefixed and `:export` may be used. That gives
493these variants: >
494
495 class ClassName
496 endclass
497
498 export class ClassName
499 endclass
500
501 abstract class ClassName
502 endclass
503
504 export abstract class ClassName
505 endclass
506<
507 *E1314*
508The class name should be CamelCased. It must start with an uppercase letter.
509That avoids clashing with builtin types.
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000510 *E1315*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000511After the class name these optional items can be used. Each can appear only
512once. They can appear in any order, although this order is recommended: >
513 extends ClassName
514 implements InterfaceName, OtherInterface
515 specifies SomeInterface
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200516< *E1355* *E1369*
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700517Each variable and method name can be used only once. It is not possible to
518define a method with the same name and different type of arguments. It is not
519possible to use a public and private member variable with the same name. A
520object variable name used in a super class cannot be reused in a child class.
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000521
522
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700523Object Variable Initialization ~
524If the type of a variable is not explicitly specified in a class, then it is
525set to "any" during class definition. When an object is instantiated from the
526class, then the type of the variable is set.
Yegappan Lakshmanan618e47d2023-08-22 21:29:28 +0200527
528
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000529Extending a class ~
530 *extends*
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000531A class can extend one other class. *E1352* *E1353* *E1354*
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000532The basic idea is to build on top of an existing class, add properties to it.
533
534The extended class is called the "base class" or "super class". The new class
535is called the "child class".
536
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700537Object variables from the base class are all taken over by the child class. It
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000538is not possible to override them (unlike some other languages).
539
540 *E1356* *E1357* *E1358*
541Object methods of the base class can be overruled. The signature (arguments,
542argument types and return type) must be exactly the same. The method of the
543base class can be called by prefixing "super.".
544
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200545 *E1377*
546The access level of a method (public or private) in a child class should be
547the same as the super class.
548
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000549Other object methods of the base class are taken over by the child class.
550
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700551Class methods, including methods starting with "new", can be overruled, like
552with object methods. The method on the base class can be called by prefixing
553the name of the class (for class methods) or "super.".
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000554
555Unlike other languages, the constructor of the base class does not need to be
556invoked. In fact, it cannot be invoked. If some initialization from the base
557class also needs to be done in a child class, put it in an object method and
558call that method from every constructor().
559
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700560If the base class did not specify a new() method then one was automatically
561created. This method will not be taken over by the child class. The child
562class can define its own new() method, or, if there isn't one, a new() method
563will be added automatically.
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000564
565
566A class implementing an interface ~
Yegappan Lakshmanan2dede3d2023-09-27 19:02:01 +0200567 *implements* *E1346* *E1347* *E1389*
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000568A class can implement one or more interfaces. The "implements" keyword can
569only appear once *E1350* . Multiple interfaces can be specified, separated by
570commas. Each interface name can appear only once. *E1351*
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000571
572
573A class defining an interface ~
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000574 *specifies*
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700575A class can declare its interface, the object variables and methods, with a
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000576named interface. This avoids the need for separately specifying the
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000577interface, which is often done in many languages, especially Java.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000578
579
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000580Items in a class ~
Yegappan Lakshmanan2dede3d2023-09-27 19:02:01 +0200581 *E1318* *E1325* *E1388*
Bram Moolenaardd60c362023-02-27 15:49:53 +0000582Inside a class, in between `:class` and `:endclass`, these items can appear:
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700583- An object variable declaration: >
584 this._privateVariableName: memberType
585 this.readonlyVariableName: memberType
586 public this.readwriteVariableName: memberType
587- A class variable declaration: >
588 static _privateClassVariableName: memberType
589 static readonlyClassVariableName: memberType
590 static public readwriteClassVariableName: memberType
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000591- A constructor method: >
Bram Moolenaar938ae282023-02-20 20:44:55 +0000592 def new(arguments)
593 def newName(arguments)
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200594- A class method: >
595 static def SomeMethod(arguments)
596 static def _PrivateMethod(arguments)
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000597- An object method: >
Bram Moolenaar938ae282023-02-20 20:44:55 +0000598 def SomeMethod(arguments)
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200599 def _PrivateMethod(arguments)
600
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700601For the object variable the type must be specified. The best way is to do
602this explicitly with ": {type}". For simple types you can also use an
603initializer, such as "= 123", and Vim will see that the type is a number.
604Avoid doing this for more complex types and when the type will be incomplete.
605For example: >
Bram Moolenaarf1dcd142022-12-31 15:30:45 +0000606 this.nameList = []
607This specifies a list, but the item type is unknown. Better use: >
608 this.nameList: list<string>
609The initialization isn't needed, the list is empty by default.
610 *E1330*
611Some types cannot be used, such as "void", "null" and "v:none".
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000612
613
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000614Defining an interface ~
615 *:interface* *:endinterface*
616An interface is defined between `:interface` and `:endinterface`. It may be
617prefixed with `:export`: >
618
619 interface InterfaceName
620 endinterface
621
622 export interface InterfaceName
623 endinterface
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000624< *E1344*
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700625An interface can declare object variables, just like in a class but without
626any initializer.
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000627 *E1345*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000628An interface can declare methods with `:def`, including the arguments and
629return type, but without the body and without `:enddef`. Example: >
630
631 interface HasSurface
632 this.size: number
633 def Surface(): number
634 endinterface
635
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000636An interface name must start with an uppercase letter. *E1343*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000637The "Has" prefix can be used to make it easier to guess this is an interface
638name, with a hint about what it provides.
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000639An interface can only be defined in a |Vim9| script file. *E1342*
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200640An interface cannot "implement" another interface but it can "extend" another
641interface. *E1381*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000642
643
Bram Moolenaar938ae282023-02-20 20:44:55 +0000644null object ~
645
Bram Moolenaardd60c362023-02-27 15:49:53 +0000646When a variable is declared to have the type of an object, but it is not
Bram Moolenaar938ae282023-02-20 20:44:55 +0000647initialized, the value is null. When trying to use this null object Vim often
648does not know what class was supposed to be used. Vim then cannot check if
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700649a variable name is correct and you will get an "Using a null object" error,
650even when the variable name is invalid. *E1360* *E1362* *E1363*
Bram Moolenaar938ae282023-02-20 20:44:55 +0000651
652
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000653Default constructor ~
654
655In case you define a class without a new() method, one will be automatically
656defined. This default constructor will have arguments for all the object
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700657variables, in the order they were specified. Thus if your class looks like: >
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000658
659 class AutoNew
660 this.name: string
661 this.age: number
662 this.gender: Gender
663 endclass
664
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700665Then the default constructor will be: >
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000666
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000667 def new(this.name = v:none, this.age = v:none, this.gender = v:none)
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000668 enddef
669
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000670The "= v:none" default values make the arguments optional. Thus you can also
671call `new()` without any arguments. No assignment will happen and the default
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700672value for the object variables will be used. This is a more useful example,
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000673with default values: >
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000674
675 class TextPosition
676 this.lnum: number = 1
677 this.col: number = 1
678 endclass
679
680If you want the constructor to have mandatory arguments, you need to write it
681yourself. For example, if for the AutoNew class above you insist on getting
682the name, you can define the constructor like this: >
683
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000684 def new(this.name, this.age = v:none, this.gender = v:none)
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000685 enddef
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000686< *E1328*
687Note that you cannot use another default value than "v:none" here. If you
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700688want to initialize the object variables, do it where they are declared. This
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000689way you only need to look in one place for the default values.
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000690
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700691All object variables will be used in the default constructor, also private
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000692access ones.
693
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700694If the class extends another one, the object variables of that class will come
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000695first.
696
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000697
698Multiple constructors ~
699
700Normally a class has just one new() constructor. In case you find that the
701constructor is often called with the same arguments you may want to simplify
702your code by putting those arguments into a second constructor method. For
703example, if you tend to use the color black a lot: >
704
705 def new(this.garment, this.color, this.size)
706 enddef
707 ...
708 var pants = new(Garment.pants, Color.black, "XL")
709 var shirt = new(Garment.shirt, Color.black, "XL")
710 var shoes = new(Garment.shoes, Color.black, "45")
711
712Instead of repeating the color every time you can add a constructor that
713includes it: >
714
715 def newBlack(this.garment, this.size)
716 this.color = Color.black
717 enddef
718 ...
719 var pants = newBlack(Garment.pants, "XL")
720 var shirt = newBlack(Garment.shirt, "XL")
721 var shoes = newBlack(Garment.shoes, "9.5")
722
723Note that the method name must start with "new". If there is no method called
724"new()" then the default constructor is added, even though there are other
725constructor methods.
726
727
728==============================================================================
729
Bram Moolenaarbe4e0162023-02-02 13:59:48 +00007307. Type definition *Vim9-type* *:type*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000731
732A type definition is giving a name to a type specification. For Example: >
733
734 :type ListOfStrings list<string>
735
736TODO: more explanation
737
738
739==============================================================================
740
Bram Moolenaarbe4e0162023-02-02 13:59:48 +00007418. Enum *Vim9-enum* *:enum* *:endenum*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000742
743An enum is a type that can have one of a list of values. Example: >
744
745 :enum Color
746 White
747 Red
748 Green
749 Blue
750 Black
751 :endenum
752
753TODO: more explanation
754
755
756==============================================================================
757
7589. Rationale
759
760Most of the choices for |Vim9| classes come from popular and recently
761developed languages, such as Java, TypeScript and Dart. The syntax has been
762made to fit with the way Vim script works, such as using `endclass` instead of
763using curly braces around the whole class.
764
765Some common constructs of object-oriented languages were chosen very long ago
766when this kind of programming was still new, and later found to be
767sub-optimal. By this time those constructs were widely used and changing them
768was not an option. In Vim we do have the freedom to make different choices,
769since classes are completely new. We can make the syntax simpler and more
770consistent than what "old" languages use. Without diverting too much, it
771should still mostly look like what you know from existing languages.
772
773Some recently developed languages add all kinds of fancy features that we
774don't need for Vim. But some have nice ideas that we do want to use.
775Thus we end up with a base of what is common in popular languages, dropping
776what looks like a bad idea, and adding some nice features that are easy to
777understand.
778
779The main rules we use to make decisions:
780- Keep it simple.
781- No surprises, mostly do what other languages are doing.
782- Avoid mistakes from the past.
783- Avoid the need for the script writer to consult the help to understand how
784 things work, most things should be obvious.
785- Keep it consistent.
786- Aim at an average size plugin, not at a huge project.
787
788
789Using new() for the constructor ~
790
791Many languages use the class name for the constructor method. A disadvantage
792is that quite often this is a long name. And when changing the class name all
793constructor methods need to be renamed. Not a big deal, but still a
794disadvantage.
795
796Other languages, such as TypeScript, use a specific name, such as
797"constructor()". That seems better. However, using "new" or "new()" to
798create a new object has no obvious relation with "constructor()".
799
800For |Vim9| script using the same method name for all constructors seemed like
801the right choice, and by calling it new() the relation between the caller and
802the method being called is obvious.
803
804
805No overloading of the constructor ~
806
807In Vim script, both legacy and |Vim9| script, there is no overloading of
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700808methods. That means it is not possible to use the same method name with
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000809different types of arguments. Therefore there also is only one new()
810constructor.
811
812With |Vim9| script it would be possible to support overloading, since
813arguments are typed. However, this gets complicated very quickly. Looking at
814a new() call one has to inspect the types of the arguments to know which of
815several new() methods is actually being called. And that can require
816inspecting quite a bit of code. For example, if one of the arguments is the
817return value of a method, you need to find that method to see what type it is
818returning.
819
820Instead, every constructor has to have a different name, starting with "new".
821That way multiple constructors with different arguments are possible, while it
822is very easy to see which constructor is being used. And the type of
823arguments can be properly checked.
824
825
826No overloading of methods ~
827
828Same reasoning as for the constructor: It is often not obvious what type
829arguments have, which would make it difficult to figure out what method is
830actually being called. Better just give the methods a different name, then
831type checking will make sure it works as you intended. This rules out
832polymorphism, which we don't really need anyway.
833
834
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000835Single inheritance and interfaces ~
836
837Some languages support multiple inheritance. Although that can be useful in
838some cases, it makes the rules of how a class works quite complicated.
839Instead, using interfaces to declare what is supported is much simpler. The
840very popular Java language does it this way, and it should be good enough for
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000841Vim. The "keep it simple" rule applies here.
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000842
843Explicitly declaring that a class supports an interface makes it easy to see
844what a class is intended for. It also makes it possible to do proper type
845checking. When an interface is changed any class that declares to implement
846it will be checked if that change was also changed. The mechanism to assume a
847class implements an interface just because the methods happen to match is
848brittle and leads to obscure problems, let's not do that.
849
850
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700851Using "this.variable" everywhere ~
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000852
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700853The object variables in various programming languages can often be accessed in
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000854different ways, depending on the location. Sometimes "this." has to be
855prepended to avoid ambiguity. They are usually declared without "this.".
856That is quite inconsistent and sometimes confusing.
857
858A very common issue is that in the constructor the arguments use the same name
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700859as the object variable. Then for these variables "this." needs to be prefixed
860in the body, while for other variables this is not needed and often omitted.
861This leads to a mix of variables with and without "this.", which is
862inconsistent.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000863
864For |Vim9| classes the "this." prefix is always used. Also for declaring the
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700865variables. Simple and consistent. When looking at the code inside a class
866it's also directly clear which variable references are object variables and
867which aren't.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000868
869
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700870Using class variables ~
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000871
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700872Using "static variable" to declare a class variable is very common, nothing
873new here. In |Vim9| script these can be accessed directly by their name.
874Very much like how a script-local variable can be used in a method. Since
875object variables are always accessed with "this." prepended, it's also quickly
876clear what kind of variable it is.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000877
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700878TypeScript prepends the class name before the class variable name, also inside
879the class. This has two problems: The class name can be rather long, taking
880up quite a bit of space, and when the class is renamed all these places need
881to be changed too.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000882
883
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700884Declaring object and class variables ~
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000885
886The main choice is whether to use "var" as with variable declarations.
887TypeScript does not use it: >
888 class Point {
889 x: number;
890 y = 0;
891 }
892
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700893Following that Vim object variables could be declared like this: >
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000894 class Point
895 this.x: number
896 this.y = 0
897 endclass
898
899Some users pointed out that this looks more like an assignment than a
900declaration. Adding "var" changes that: >
901 class Point
902 var this.x: number
903 var this.y = 0
904 endclass
905
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700906We also need to be able to declare class variables using the "static" keyword.
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000907There we can also choose to leave out "var": >
908 class Point
909 var this.x: number
910 static count = 0
911 endclass
912
913Or do use it, before "static": >
914 class Point
915 var this.x: number
916 var static count = 0
917 endclass
918
919Or after "static": >
920 class Point
921 var this.x: number
922 static var count = 0
923 endclass
924
925This is more in line with "static def Func()".
926
927There is no clear preference whether to use "var" or not. The two main
928reasons to leave it out are:
9291. TypeScript, Java and other popular languages do not use it.
9302. Less clutter.
931
932
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000933Using "ClassName.new()" to construct an object ~
934
935Many languages use the "new" operator to create an object, which is actually
936kind of strange, since the constructor is defined as a method with arguments,
937not a command. TypeScript also has the "new" keyword, but the method is
938called "constructor()", it is hard to see the relation between the two.
939
940In |Vim9| script the constructor method is called new(), and it is invoked as
941new(), simple and straightforward. Other languages use "new ClassName()",
942while there is no ClassName() method, it's a method by another name in the
943class called ClassName. Quite confusing.
944
945
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700946Default read access to object variables ~
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000947
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700948Some users will remark that the access rules for object variables are
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000949asymmetric. Well, that is intentional. Changing a value is a very different
950action than reading a value. The read operation has no side effects, it can
951be done any number of times without affecting the object. Changing the value
952can have many side effects, and even have a ripple effect, affecting other
953objects.
954
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700955When adding object variables one usually doesn't think much about this, just
956get the type right. And normally the values are set in the new() method.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000957Therefore defaulting to read access only "just works" in most cases. And when
958directly writing you get an error, which makes you wonder if you actually want
959to allow that. This helps writing code with fewer mistakes.
960
961
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700962Making object variables private with an underscore ~
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000963
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700964When an object variable is private, it can only be read and changed inside the
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000965class (and in sub-classes), then it cannot be used outside of the class.
966Prepending an underscore is a simple way to make that visible. Various
967programming languages have this as a recommendation.
968
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700969In case you change your mind and want to make the object variable accessible
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000970outside of the class, you will have to remove the underscore everywhere.
971Since the name only appears in the class (and sub-classes) they will be easy
972to find and change.
973
974The other way around is much harder: you can easily prepend an underscore to
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700975the object variable inside the class to make it private, but any usage
976elsewhere you will have to track down and change. You may have to make it a
977"set" method call. This reflects the real world problem that taking away
978access requires work to be done for all places where that access exists.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000979
980An alternative would have been using the "private" keyword, just like "public"
981changes the access in the other direction. Well, that's just to reduce the
982number of keywords.
983
984
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700985No protected object variables ~
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000986
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700987Some languages provide several ways to control access to object variables.
988The most known is "protected", and the meaning varies from language to
989language. Others are "shared", "private" and even "friend".
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000990
991These rules make life more difficult. That can be justified in projects where
992many people work on the same, complex code where it is easy to make mistakes.
993Especially when refactoring or other changes to the class model.
994
995The Vim scripts are expected to be used in a plugin, with just one person or a
996small team working on it. Complex rules then only make it more complicated,
Bram Moolenaar71badf92023-04-22 22:40:14 +0100997the extra safety provided by the rules isn't really needed. Let's just keep
998it simple and not specify access details.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000999
1000
1001==============================================================================
1002
100310. To be done later
1004
1005Can a newSomething() constructor invoke another constructor? If yes, what are
1006the restrictions?
1007
1008Thoughts:
1009- Generics for a class: `class <Tkey, Tentry>`
1010- Generics for a function: `def <Tkey> GetLast(key: Tkey)`
1011- Mixins: not sure if that is useful, leave out for simplicity.
1012
1013Some things that look like good additions:
1014- For testing: Mock mechanism
1015
1016An important class to be provided is "Promise". Since Vim is single
1017threaded, connecting asynchronous operations is a natural way of allowing
1018plugins to do their work without blocking the user. It's a uniform way to
1019invoke callbacks and handle timeouts and errors.
1020
1021
1022 vim:tw=78:ts=8:noet:ft=help:norl: