blob: a27f7319a7f8b5383b27d05bc99d9da7ec4272a1 [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
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000219
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700220Not only is this text you need to write, it also has the type of each
221variables twice. Since this is so common a shorter way to write new() is
222provided: >
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000223
224 def new(this.lnum, this.col)
225 enddef
226
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700227The semantics are easy to understand: Providing the object variable name,
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000228including "this.", as the argument to new() means the value provided in the
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700229new() call is assigned to that object variable. This mechanism comes from the
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000230Dart language.
231
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700232Putting together this way of using new() and making the variables public
233results in a much shorter class definition than what we started with: >
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000234
235 class TextPosition
236 public this.lnum: number
237 public this.col: number
238
239 def new(this.lnum, this.col)
240 enddef
241
242 def SetPosition(lnum: number, col: number)
243 this.lnum = lnum
244 this.col = col
245 enddef
246 endclass
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000247
248The sequence of constructing a new object is:
2491. Memory is allocated and cleared. All values are zero/false/empty.
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -07002502. For each declared object variable that has an initializer, the expression
251 is evaluated and assigned to the variable. This happens in the sequence
252 the variables are declared in the class.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +00002533. Arguments in the new() method in the "this.name" form are assigned.
2544. The body of the new() method is executed.
255
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000256If the class extends a parent class, the same thing happens. In the second
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700257step the object variables of the parent class are initialized first. There is
258no need to call "super()" or "new()" on the parent.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000259
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200260 *E1365*
Yegappan Lakshmanan6ac15442023-08-20 18:20:17 +0200261When defining the new() method the return type should not be specified. It
262always returns an object of the class.
263
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000264==============================================================================
265
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02002663. Class Variables and Methods *Vim9-class-member*
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000267
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200268 *:static* *E1337* *E1338* *E1368*
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000269Class members are declared with "static". They are used by the name without a
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200270prefix in the class where they are defined: >
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000271
272 class OtherThing
273 this.size: number
274 static totalSize: number
275
276 def new(this.size)
277 totalSize += this.size
278 enddef
279 endclass
280< *E1340* *E1341*
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700281Since the name is used as-is, shadowing the name by a method argument name
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000282or local variable name is not allowed.
283
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200284 *E1374* *E1375*
285To access a class member outside of the class where it is defined, the class
286name prefix must be used. A class member cannot be accessed using an object.
287
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000288Just like object members the access can be made private by using an underscore
289as the first character in the name, and it can be made public by prefixing
290"public": >
291
292 class OtherThing
293 static total: number # anybody can read, only class can write
294 static _sum: number # only class can read and write
295 public static result: number # anybody can read and write
296 endclass
297<
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200298 *class-method*
299Class methods are also declared with "static". They can use the class
300variables but they have no access to the object variables, they cannot use the
301"this" keyword.
302>
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000303 class OtherThing
304 this.size: number
305 static totalSize: number
306
307 # Clear the total size and return the value it had before.
308 static def ClearTotalSize(): number
309 var prev = totalSize
310 totalSize = 0
311 return prev
312 enddef
313 endclass
314
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200315Inside the class the class method can be called by name directly, outside the
316class the class name must be prefixed: `OtherThing.ClearTotalSize()`. To use
317a super class method in a child class, the class name must be prefixed.
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000318
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +0200319Just like object methods the access can be made private by using an underscore
320as the first character in the method name: >
321
322 class OtherThing
323 static def _Foo()
324 echo "Foo"
325 enddef
326 def Bar()
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200327 _Foo()
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +0200328 enddef
329 endclass
Gianmaria Bajo4b9777a2023-08-29 22:26:30 +0200330<
331 *E1370*
332Note that constructors cannot be declared as "static", because they always
333are.
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +0200334
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200335To access the class methods and class variables of a super class in an
336extended class, the class name prefix should be used just as from anywhere
337outside of the defining class: >
338
339 vim9script
340 class Vehicle
341 static nextID: number = 1000
342 static def GetID(): number
343 nextID += 1
344 return nextID
345 enddef
346 endclass
347 class Car extends Vehicle
348 this.myID: number
349 def new()
350 this.myID = Vehicle.GetID()
351 enddef
352 endclass
353<
354Class variables and methods are not inherited by a child class. A child class
355can declare a static variable or a method with the same name as the one in the
356super class. Depending on the class where the member is used the
357corresponding class member will be used. The type of the class member in a
358child class can be different from that in the super class.
359
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000360==============================================================================
361
3624. Using an abstract class *Vim9-abstract-class*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000363
364An abstract class forms the base for at least one sub-class. In the class
365model one often finds that a few classes have the same properties that can be
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000366shared, but a class with these properties does not have enough state to create
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000367an object from. A sub-class must extend the abstract class and add the
368missing state and/or methods before it can be used to create objects for.
369
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000370For example, a Shape class could store a color and thickness. You cannot
371create a Shape object, it is missing the information about what kind of shape
372it is. The Shape class functions as the base for a Square and a Triangle
373class, for which objects can be created. Example: >
374
375 abstract class Shape
376 this.color = Color.Black
377 this.thickness = 10
378 endclass
379
380 class Square extends Shape
381 this.size: number
382
383 def new(this.size)
384 enddef
385 endclass
386
387 class Triangle extends Shape
388 this.base: number
389 this.height: number
390
391 def new(this.base, this.height)
392 enddef
393 endclass
394<
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000395An abstract class is defined the same way as a normal class, except that it
396does not have any new() method. *E1359*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000397
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200398 *abstract-method* *E1371* *E1372*
Yegappan Lakshmanan7bcd25c2023-09-08 19:27:51 +0200399An abstract method can be defined in an abstract class by using the "abstract"
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700400prefix when defining the method: >
Yegappan Lakshmanan7bcd25c2023-09-08 19:27:51 +0200401
402 abstract class Shape
403 abstract def Draw()
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200404 abstract static def SetColor()
Yegappan Lakshmanan7bcd25c2023-09-08 19:27:51 +0200405 endclass
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200406<
407 *E1373*
Yegappan Lakshmanan7bcd25c2023-09-08 19:27:51 +0200408A class extending the abstract class must implement all the abstract methods.
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200409The signature (arguments, argument types and return type) must be exactly the
410same. Class methods in an abstract class can also be abstract methods.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000411
412==============================================================================
413
Bram Moolenaarbe4e0162023-02-02 13:59:48 +00004145. Using an interface *Vim9-using-interface*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000415
416The example above with Shape, Square and Triangle can be made more useful if
417we add a method to compute the surface of the object. For that we create the
418interface called HasSurface, which specifies one method Surface() that returns
419a number. This example extends the one above: >
420
421 abstract class Shape
422 this.color = Color.Black
423 this.thickness = 10
424 endclass
425
426 interface HasSurface
427 def Surface(): number
428 endinterface
429
430 class Square extends Shape implements HasSurface
431 this.size: number
432
433 def new(this.size)
434 enddef
435
436 def Surface(): number
437 return this.size * this.size
438 enddef
439 endclass
440
441 class Triangle extends Shape implements HasSurface
442 this.base: number
443 this.height: number
444
445 def new(this.base, this.height)
446 enddef
447
448 def Surface(): number
449 return this.base * this.height / 2
450 enddef
451 endclass
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200452<
453 *E1348* *E1349* *E1367* *E1382* *E1383*
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000454If a class declares to implement an interface, all the items specified in the
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200455interface must appear in the class, with the same types.
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000456
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000457The interface name can be used as a type: >
458
459 var shapes: list<HasSurface> = [
460 Square.new(12),
461 Triangle.new(8, 15),
462 ]
463 for shape in shapes
464 echo $'the surface is {shape.Surface()}'
465 endfor
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200466<
467 *E1378* *E1379* *E1380*
468An interface can have only instance variables (read-only and read-write
469access) and methods. An interface cannot contain private variables, private
470methods, class variables and class methods.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000471
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200472An interface can extend another interface using "extends". The sub-interface
473inherits all the instance variables and methods from the super interface.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000474
475==============================================================================
476
Bram Moolenaarbe4e0162023-02-02 13:59:48 +00004776. More class details *Vim9-class* *Class* *class*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000478
479Defining a class ~
480 *:class* *:endclass* *:abstract*
481A class is defined between `:class` and `:endclass`. The whole class is
482defined in one script file. It is not possible to add to a class later.
483
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000484A class can only be defined in a |Vim9| script file. *E1316*
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000485A class cannot be defined inside a function.
486
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000487It is possible to define more than one class in a script file. Although it
488usually is better to export only one main class. It can be useful to define
489types, enums and helper classes though.
490
491The `:abstract` keyword may be prefixed and `:export` may be used. That gives
492these variants: >
493
494 class ClassName
495 endclass
496
497 export class ClassName
498 endclass
499
500 abstract class ClassName
501 endclass
502
503 export abstract class ClassName
504 endclass
505<
506 *E1314*
507The class name should be CamelCased. It must start with an uppercase letter.
508That avoids clashing with builtin types.
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000509 *E1315*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000510After the class name these optional items can be used. Each can appear only
511once. They can appear in any order, although this order is recommended: >
512 extends ClassName
513 implements InterfaceName, OtherInterface
514 specifies SomeInterface
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200515< *E1355* *E1369*
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700516Each variable and method name can be used only once. It is not possible to
517define a method with the same name and different type of arguments. It is not
518possible to use a public and private member variable with the same name. A
519object variable name used in a super class cannot be reused in a child class.
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000520
521
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700522Object Variable Initialization ~
523If the type of a variable is not explicitly specified in a class, then it is
524set to "any" during class definition. When an object is instantiated from the
525class, then the type of the variable is set.
Yegappan Lakshmanan618e47d2023-08-22 21:29:28 +0200526
527
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000528Extending a class ~
529 *extends*
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000530A class can extend one other class. *E1352* *E1353* *E1354*
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000531The basic idea is to build on top of an existing class, add properties to it.
532
533The extended class is called the "base class" or "super class". The new class
534is called the "child class".
535
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700536Object variables from the base class are all taken over by the child class. It
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000537is not possible to override them (unlike some other languages).
538
539 *E1356* *E1357* *E1358*
540Object methods of the base class can be overruled. The signature (arguments,
541argument types and return type) must be exactly the same. The method of the
542base class can be called by prefixing "super.".
543
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200544 *E1377*
545The access level of a method (public or private) in a child class should be
546the same as the super class.
547
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000548Other object methods of the base class are taken over by the child class.
549
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700550Class methods, including methods starting with "new", can be overruled, like
551with object methods. The method on the base class can be called by prefixing
552the name of the class (for class methods) or "super.".
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000553
554Unlike other languages, the constructor of the base class does not need to be
555invoked. In fact, it cannot be invoked. If some initialization from the base
556class also needs to be done in a child class, put it in an object method and
557call that method from every constructor().
558
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700559If the base class did not specify a new() method then one was automatically
560created. This method will not be taken over by the child class. The child
561class can define its own new() method, or, if there isn't one, a new() method
562will be added automatically.
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000563
564
565A class implementing an interface ~
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000566 *implements* *E1346* *E1347*
567A class can implement one or more interfaces. The "implements" keyword can
568only appear once *E1350* . Multiple interfaces can be specified, separated by
569commas. Each interface name can appear only once. *E1351*
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000570
571
572A class defining an interface ~
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000573 *specifies*
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700574A class can declare its interface, the object variables and methods, with a
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000575named interface. This avoids the need for separately specifying the
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000576interface, which is often done in many languages, especially Java.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000577
578
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000579Items in a class ~
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200580 *E1318* *E1325*
Bram Moolenaardd60c362023-02-27 15:49:53 +0000581Inside a class, in between `:class` and `:endclass`, these items can appear:
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700582- An object variable declaration: >
583 this._privateVariableName: memberType
584 this.readonlyVariableName: memberType
585 public this.readwriteVariableName: memberType
586- A class variable declaration: >
587 static _privateClassVariableName: memberType
588 static readonlyClassVariableName: memberType
589 static public readwriteClassVariableName: memberType
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000590- A constructor method: >
Bram Moolenaar938ae282023-02-20 20:44:55 +0000591 def new(arguments)
592 def newName(arguments)
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200593- A class method: >
594 static def SomeMethod(arguments)
595 static def _PrivateMethod(arguments)
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000596- An object method: >
Bram Moolenaar938ae282023-02-20 20:44:55 +0000597 def SomeMethod(arguments)
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200598 def _PrivateMethod(arguments)
599
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700600For the object variable the type must be specified. The best way is to do
601this explicitly with ": {type}". For simple types you can also use an
602initializer, such as "= 123", and Vim will see that the type is a number.
603Avoid doing this for more complex types and when the type will be incomplete.
604For example: >
Bram Moolenaarf1dcd142022-12-31 15:30:45 +0000605 this.nameList = []
606This specifies a list, but the item type is unknown. Better use: >
607 this.nameList: list<string>
608The initialization isn't needed, the list is empty by default.
609 *E1330*
610Some types cannot be used, such as "void", "null" and "v:none".
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000611
612
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000613Defining an interface ~
614 *:interface* *:endinterface*
615An interface is defined between `:interface` and `:endinterface`. It may be
616prefixed with `:export`: >
617
618 interface InterfaceName
619 endinterface
620
621 export interface InterfaceName
622 endinterface
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000623< *E1344*
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700624An interface can declare object variables, just like in a class but without
625any initializer.
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000626 *E1345*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000627An interface can declare methods with `:def`, including the arguments and
628return type, but without the body and without `:enddef`. Example: >
629
630 interface HasSurface
631 this.size: number
632 def Surface(): number
633 endinterface
634
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000635An interface name must start with an uppercase letter. *E1343*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000636The "Has" prefix can be used to make it easier to guess this is an interface
637name, with a hint about what it provides.
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000638An interface can only be defined in a |Vim9| script file. *E1342*
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +0200639An interface cannot "implement" another interface but it can "extend" another
640interface. *E1381*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000641
642
Bram Moolenaar938ae282023-02-20 20:44:55 +0000643null object ~
644
Bram Moolenaardd60c362023-02-27 15:49:53 +0000645When a variable is declared to have the type of an object, but it is not
Bram Moolenaar938ae282023-02-20 20:44:55 +0000646initialized, the value is null. When trying to use this null object Vim often
647does not know what class was supposed to be used. Vim then cannot check if
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700648a variable name is correct and you will get an "Using a null object" error,
649even when the variable name is invalid. *E1360* *E1362* *E1363*
Bram Moolenaar938ae282023-02-20 20:44:55 +0000650
651
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000652Default constructor ~
653
654In case you define a class without a new() method, one will be automatically
655defined. This default constructor will have arguments for all the object
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700656variables, in the order they were specified. Thus if your class looks like: >
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000657
658 class AutoNew
659 this.name: string
660 this.age: number
661 this.gender: Gender
662 endclass
663
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700664Then the default constructor will be: >
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000665
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000666 def new(this.name = v:none, this.age = v:none, this.gender = v:none)
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000667 enddef
668
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000669The "= v:none" default values make the arguments optional. Thus you can also
670call `new()` without any arguments. No assignment will happen and the default
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700671value for the object variables will be used. This is a more useful example,
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000672with default values: >
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000673
674 class TextPosition
675 this.lnum: number = 1
676 this.col: number = 1
677 endclass
678
679If you want the constructor to have mandatory arguments, you need to write it
680yourself. For example, if for the AutoNew class above you insist on getting
681the name, you can define the constructor like this: >
682
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000683 def new(this.name, this.age = v:none, this.gender = v:none)
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000684 enddef
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000685< *E1328*
686Note that you cannot use another default value than "v:none" here. If you
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700687want to initialize the object variables, do it where they are declared. This
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000688way you only need to look in one place for the default values.
Bram Moolenaar7db29e42022-12-11 15:53:04 +0000689
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700690All object variables will be used in the default constructor, also private
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000691access ones.
692
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700693If the class extends another one, the object variables of that class will come
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000694first.
695
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000696
697Multiple constructors ~
698
699Normally a class has just one new() constructor. In case you find that the
700constructor is often called with the same arguments you may want to simplify
701your code by putting those arguments into a second constructor method. For
702example, if you tend to use the color black a lot: >
703
704 def new(this.garment, this.color, this.size)
705 enddef
706 ...
707 var pants = new(Garment.pants, Color.black, "XL")
708 var shirt = new(Garment.shirt, Color.black, "XL")
709 var shoes = new(Garment.shoes, Color.black, "45")
710
711Instead of repeating the color every time you can add a constructor that
712includes it: >
713
714 def newBlack(this.garment, this.size)
715 this.color = Color.black
716 enddef
717 ...
718 var pants = newBlack(Garment.pants, "XL")
719 var shirt = newBlack(Garment.shirt, "XL")
720 var shoes = newBlack(Garment.shoes, "9.5")
721
722Note that the method name must start with "new". If there is no method called
723"new()" then the default constructor is added, even though there are other
724constructor methods.
725
726
727==============================================================================
728
Bram Moolenaarbe4e0162023-02-02 13:59:48 +00007297. Type definition *Vim9-type* *:type*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000730
731A type definition is giving a name to a type specification. For Example: >
732
733 :type ListOfStrings list<string>
734
735TODO: more explanation
736
737
738==============================================================================
739
Bram Moolenaarbe4e0162023-02-02 13:59:48 +00007408. Enum *Vim9-enum* *:enum* *:endenum*
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000741
742An enum is a type that can have one of a list of values. Example: >
743
744 :enum Color
745 White
746 Red
747 Green
748 Blue
749 Black
750 :endenum
751
752TODO: more explanation
753
754
755==============================================================================
756
7579. Rationale
758
759Most of the choices for |Vim9| classes come from popular and recently
760developed languages, such as Java, TypeScript and Dart. The syntax has been
761made to fit with the way Vim script works, such as using `endclass` instead of
762using curly braces around the whole class.
763
764Some common constructs of object-oriented languages were chosen very long ago
765when this kind of programming was still new, and later found to be
766sub-optimal. By this time those constructs were widely used and changing them
767was not an option. In Vim we do have the freedom to make different choices,
768since classes are completely new. We can make the syntax simpler and more
769consistent than what "old" languages use. Without diverting too much, it
770should still mostly look like what you know from existing languages.
771
772Some recently developed languages add all kinds of fancy features that we
773don't need for Vim. But some have nice ideas that we do want to use.
774Thus we end up with a base of what is common in popular languages, dropping
775what looks like a bad idea, and adding some nice features that are easy to
776understand.
777
778The main rules we use to make decisions:
779- Keep it simple.
780- No surprises, mostly do what other languages are doing.
781- Avoid mistakes from the past.
782- Avoid the need for the script writer to consult the help to understand how
783 things work, most things should be obvious.
784- Keep it consistent.
785- Aim at an average size plugin, not at a huge project.
786
787
788Using new() for the constructor ~
789
790Many languages use the class name for the constructor method. A disadvantage
791is that quite often this is a long name. And when changing the class name all
792constructor methods need to be renamed. Not a big deal, but still a
793disadvantage.
794
795Other languages, such as TypeScript, use a specific name, such as
796"constructor()". That seems better. However, using "new" or "new()" to
797create a new object has no obvious relation with "constructor()".
798
799For |Vim9| script using the same method name for all constructors seemed like
800the right choice, and by calling it new() the relation between the caller and
801the method being called is obvious.
802
803
804No overloading of the constructor ~
805
806In Vim script, both legacy and |Vim9| script, there is no overloading of
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700807methods. That means it is not possible to use the same method name with
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000808different types of arguments. Therefore there also is only one new()
809constructor.
810
811With |Vim9| script it would be possible to support overloading, since
812arguments are typed. However, this gets complicated very quickly. Looking at
813a new() call one has to inspect the types of the arguments to know which of
814several new() methods is actually being called. And that can require
815inspecting quite a bit of code. For example, if one of the arguments is the
816return value of a method, you need to find that method to see what type it is
817returning.
818
819Instead, every constructor has to have a different name, starting with "new".
820That way multiple constructors with different arguments are possible, while it
821is very easy to see which constructor is being used. And the type of
822arguments can be properly checked.
823
824
825No overloading of methods ~
826
827Same reasoning as for the constructor: It is often not obvious what type
828arguments have, which would make it difficult to figure out what method is
829actually being called. Better just give the methods a different name, then
830type checking will make sure it works as you intended. This rules out
831polymorphism, which we don't really need anyway.
832
833
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000834Single inheritance and interfaces ~
835
836Some languages support multiple inheritance. Although that can be useful in
837some cases, it makes the rules of how a class works quite complicated.
838Instead, using interfaces to declare what is supported is much simpler. The
839very popular Java language does it this way, and it should be good enough for
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000840Vim. The "keep it simple" rule applies here.
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000841
842Explicitly declaring that a class supports an interface makes it easy to see
843what a class is intended for. It also makes it possible to do proper type
844checking. When an interface is changed any class that declares to implement
845it will be checked if that change was also changed. The mechanism to assume a
846class implements an interface just because the methods happen to match is
847brittle and leads to obscure problems, let's not do that.
848
849
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700850Using "this.variable" everywhere ~
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000851
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700852The object variables in various programming languages can often be accessed in
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000853different ways, depending on the location. Sometimes "this." has to be
854prepended to avoid ambiguity. They are usually declared without "this.".
855That is quite inconsistent and sometimes confusing.
856
857A very common issue is that in the constructor the arguments use the same name
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700858as the object variable. Then for these variables "this." needs to be prefixed
859in the body, while for other variables this is not needed and often omitted.
860This leads to a mix of variables with and without "this.", which is
861inconsistent.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000862
863For |Vim9| classes the "this." prefix is always used. Also for declaring the
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700864variables. Simple and consistent. When looking at the code inside a class
865it's also directly clear which variable references are object variables and
866which aren't.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000867
868
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700869Using class variables ~
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000870
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700871Using "static variable" to declare a class variable is very common, nothing
872new here. In |Vim9| script these can be accessed directly by their name.
873Very much like how a script-local variable can be used in a method. Since
874object variables are always accessed with "this." prepended, it's also quickly
875clear what kind of variable it is.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000876
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700877TypeScript prepends the class name before the class variable name, also inside
878the class. This has two problems: The class name can be rather long, taking
879up quite a bit of space, and when the class is renamed all these places need
880to be changed too.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000881
882
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700883Declaring object and class variables ~
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000884
885The main choice is whether to use "var" as with variable declarations.
886TypeScript does not use it: >
887 class Point {
888 x: number;
889 y = 0;
890 }
891
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700892Following that Vim object variables could be declared like this: >
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000893 class Point
894 this.x: number
895 this.y = 0
896 endclass
897
898Some users pointed out that this looks more like an assignment than a
899declaration. Adding "var" changes that: >
900 class Point
901 var this.x: number
902 var this.y = 0
903 endclass
904
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700905We also need to be able to declare class variables using the "static" keyword.
Bram Moolenaar1b5f03e2023-01-09 20:12:45 +0000906There we can also choose to leave out "var": >
907 class Point
908 var this.x: number
909 static count = 0
910 endclass
911
912Or do use it, before "static": >
913 class Point
914 var this.x: number
915 var static count = 0
916 endclass
917
918Or after "static": >
919 class Point
920 var this.x: number
921 static var count = 0
922 endclass
923
924This is more in line with "static def Func()".
925
926There is no clear preference whether to use "var" or not. The two main
927reasons to leave it out are:
9281. TypeScript, Java and other popular languages do not use it.
9292. Less clutter.
930
931
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000932Using "ClassName.new()" to construct an object ~
933
934Many languages use the "new" operator to create an object, which is actually
935kind of strange, since the constructor is defined as a method with arguments,
936not a command. TypeScript also has the "new" keyword, but the method is
937called "constructor()", it is hard to see the relation between the two.
938
939In |Vim9| script the constructor method is called new(), and it is invoked as
940new(), simple and straightforward. Other languages use "new ClassName()",
941while there is no ClassName() method, it's a method by another name in the
942class called ClassName. Quite confusing.
943
944
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700945Default read access to object variables ~
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000946
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700947Some users will remark that the access rules for object variables are
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000948asymmetric. Well, that is intentional. Changing a value is a very different
949action than reading a value. The read operation has no side effects, it can
950be done any number of times without affecting the object. Changing the value
951can have many side effects, and even have a ripple effect, affecting other
952objects.
953
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700954When adding object variables one usually doesn't think much about this, just
955get the type right. And normally the values are set in the new() method.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000956Therefore defaulting to read access only "just works" in most cases. And when
957directly writing you get an error, which makes you wonder if you actually want
958to allow that. This helps writing code with fewer mistakes.
959
960
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700961Making object variables private with an underscore ~
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000962
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700963When an object variable is private, it can only be read and changed inside the
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000964class (and in sub-classes), then it cannot be used outside of the class.
965Prepending an underscore is a simple way to make that visible. Various
966programming languages have this as a recommendation.
967
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700968In case you change your mind and want to make the object variable accessible
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000969outside of the class, you will have to remove the underscore everywhere.
970Since the name only appears in the class (and sub-classes) they will be easy
971to find and change.
972
973The other way around is much harder: you can easily prepend an underscore to
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700974the object variable inside the class to make it private, but any usage
975elsewhere you will have to track down and change. You may have to make it a
976"set" method call. This reflects the real world problem that taking away
977access requires work to be done for all places where that access exists.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000978
979An alternative would have been using the "private" keyword, just like "public"
980changes the access in the other direction. Well, that's just to reduce the
981number of keywords.
982
983
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700984No protected object variables ~
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000985
Yegappan Lakshmananc3b315f2023-09-24 14:36:17 -0700986Some languages provide several ways to control access to object variables.
987The most known is "protected", and the meaning varies from language to
988language. Others are "shared", "private" and even "friend".
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000989
990These rules make life more difficult. That can be justified in projects where
991many people work on the same, complex code where it is easy to make mistakes.
992Especially when refactoring or other changes to the class model.
993
994The Vim scripts are expected to be used in a plugin, with just one person or a
995small team working on it. Complex rules then only make it more complicated,
Bram Moolenaar71badf92023-04-22 22:40:14 +0100996the extra safety provided by the rules isn't really needed. Let's just keep
997it simple and not specify access details.
Bram Moolenaarc1c365c2022-12-04 20:13:24 +0000998
999
1000==============================================================================
1001
100210. To be done later
1003
1004Can a newSomething() constructor invoke another constructor? If yes, what are
1005the restrictions?
1006
1007Thoughts:
1008- Generics for a class: `class <Tkey, Tentry>`
1009- Generics for a function: `def <Tkey> GetLast(key: Tkey)`
1010- Mixins: not sure if that is useful, leave out for simplicity.
1011
1012Some things that look like good additions:
1013- For testing: Mock mechanism
1014
1015An important class to be provided is "Promise". Since Vim is single
1016threaded, connecting asynchronous operations is a natural way of allowing
1017plugins to do their work without blocking the user. It's a uniform way to
1018invoke callbacks and handle timeouts and errors.
1019
1020
1021 vim:tw=78:ts=8:noet:ft=help:norl: