Mercurial > vim
annotate runtime/doc/vim9class.txt @ 33401:bb99820510ef v9.0.1959
patch 9.0.1959: Vim9: methods parameters and types are covariant
Commit: https://github.com/vim/vim/commit/f3b68d4759a040ed0c4844c279ea3c779b3863ff
Author: Yegappan Lakshmanan <yegappan@yahoo.com>
Date: Fri Sep 29 22:50:02 2023 +0200
patch 9.0.1959: Vim9: methods parameters and types are covariant
Problem: Vim9: methods parameters and types are covariant
Solution: Support contra-variant type check for object method arguments
(similar to Dart).
closes: #12965
closes: #13221
Signed-off-by: Christian Brabandt <cb@256bit.org>
Co-authored-by: Yegappan Lakshmanan <yegappan@yahoo.com>
author | Christian Brabandt <cb@256bit.org> |
---|---|
date | Fri, 29 Sep 2023 23:00:04 +0200 |
parents | b5ad84fdc702 |
children | 97ceabebaeaf |
rev | line source |
---|---|
33286
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
1 *vim9class.txt* For Vim version 9.0. Last change: 2023 Sep 18 |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
2 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
3 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
4 VIM REFERENCE MANUAL by Bram Moolenaar |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
5 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
6 |
31885 | 7 NOTE - This is not finished yet, anything can still change! - NOTE |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
8 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
9 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
10 Vim9 classes, objects, interfaces, types and enums. |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
11 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
12 1. Overview |Vim9-class-overview| |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
13 2. A simple class |Vim9-simple-class| |
33286
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
14 3. Class variables and methods |Vim9-class-member| |
31885 | 15 4. Using an abstract class |Vim9-abstract-class| |
16 5. Using an interface |Vim9-using-interface| | |
17 6. More class details |Vim9-class| | |
18 7. Type definition |Vim9-type| | |
19 8. Enum |Vim9-enum| | |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
20 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
21 9. Rationale |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
22 10. To be done later |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
23 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
24 ============================================================================== |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
25 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
26 1. Overview *Vim9-class-overview* |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
27 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
28 The fancy term is "object-oriented programming". You can find lots of study |
31885 | 29 material on this subject. Here we document what |Vim9| script provides, |
30 assuming you know the basics already. Added are helpful hints about how to | |
31 use this functionality effectively. | |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
32 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
33 The basic item is an object: |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
34 - An object stores state. It contains one or more variables that can each |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
35 have a value. |
31885 | 36 - An object provides functions that use and manipulate its state. These |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
37 functions are invoked "on the object", which is what sets it apart from the |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
38 traditional separation of data and code that manipulates the data. |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
39 - An object has a well defined interface, with typed member variables and |
33351
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
40 methods. |
31885 | 41 - Objects are created from a class and all objects have the same interface. |
42 This does not change at runtime, it is not dynamic. | |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
43 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
44 An object can only be created by a class. A class provides: |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
45 - A new() method, the constructor, which returns an object for the class. |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
46 This method is invoked on the class name: MyClass.new(). |
31885 | 47 - State shared by all objects of the class: class variables (class members). |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
48 - A hierarchy of classes, with super-classes and sub-classes, inheritance. |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
49 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
50 An interface is used to specify properties of an object: |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
51 - An object can declare several interfaces that it implements. |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
52 - Different objects implementing the same interface can be used the same way. |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
53 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
54 The class hierarchy allows for single inheritance. Otherwise interfaces are |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
55 to be used where needed. |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
56 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
57 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
58 Class modeling ~ |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
59 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
60 You can model classes any way you like. Keep in mind what you are building, |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
61 don't try to model the real world. This can be confusing, especially because |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
62 teachers use real-world objects to explain class relations and you might think |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
63 your model should therefore reflect the real world. It doesn't! The model |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
64 should match your purpose. |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
65 |
31885 | 66 Keep in mind that composition (an object contains other objects) is often |
67 better than inheritance (an object extends another object). Don't waste time | |
68 trying to find the optimal class model. Or waste time discussing whether a | |
69 square is a rectangle or that a rectangle is a square. It doesn't matter. | |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
70 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
71 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
72 ============================================================================== |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
73 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
74 2. A simple class *Vim9-simple-class* |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
75 |
31885 | 76 Let's start with a simple example: a class that stores a text position (see |
77 below for how to do this more efficiently): > | |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
78 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
79 class TextPosition |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
80 this.lnum: number |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
81 this.col: number |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
82 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
83 def new(lnum: number, col: number) |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
84 this.lnum = lnum |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
85 this.col = col |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
86 enddef |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
87 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
88 def SetLnum(lnum: number) |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
89 this.lnum = lnum |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
90 enddef |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
91 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
92 def SetCol(col: number) |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
93 this.col = col |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
94 enddef |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
95 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
96 def SetPosition(lnum: number, col: number) |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
97 this.lnum = lnum |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
98 this.col = col |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
99 enddef |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
100 endclass |
31430 | 101 < *object* *Object* |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
102 You can create an object from this class with the new() method: > |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
103 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
104 var pos = TextPosition.new(1, 1) |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
105 |
33351
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
106 The object variables "lnum" and "col" can be accessed directly: > |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
107 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
108 echo $'The text position is ({pos.lnum}, {pos.col})' |
31430 | 109 < *E1317* *E1327* |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
110 If you have been using other object-oriented languages you will notice that |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
111 in Vim the object members are consistently referred to with the "this." |
31885 | 112 prefix. This is different from languages like Java and TypeScript. The |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
113 naming convention makes the object members easy to spot. Also, when a |
33351
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
114 variable does not have the "this." prefix you know it is not an object |
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
115 variable. |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
116 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
117 |
33351
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
118 Object variable write access ~ |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
119 |
33351
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
120 Now try to change an object variable directly: > |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
121 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
122 pos.lnum = 9 |
31885 | 123 < *E1335* |
33351
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
124 This will give you an error! That is because by default object variables can |
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
125 be read but not set. That's why the TextPosition class provides a method for |
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
126 it: > |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
127 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
128 pos.SetLnum(9) |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
129 |
33351
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
130 Allowing to read but not set an object variable is the most common and safest |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
131 way. Most often there is no problem using a value, while setting a value may |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
132 have side effects that need to be taken care of. In this case, the SetLnum() |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
133 method could check if the line number is valid and either give an error or use |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
134 the closest valid value. |
31885 | 135 *:public* *E1331* |
33351
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
136 If you don't care about side effects and want to allow the object variable to |
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
137 be changed at any time, you can make it public: > |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
138 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
139 public this.lnum: number |
31885 | 140 public this.col: number |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
141 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
142 Now you don't need the SetLnum(), SetCol() and SetPosition() methods, setting |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
143 "pos.lnum" directly above will no longer give an error. |
33286
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
144 *E1326* |
33351
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
145 If you try to set an object variable that doesn't exist you get an error: > |
31579 | 146 pos.other = 9 |
33286
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
147 < E1326: Member not found on object "TextPosition": other ~ |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
148 |
33286
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
149 *E1376* |
33351
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
150 A object variable cannot be accessed using the class name. |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
151 |
33351
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
152 Private variables ~ |
33383
5c12ca4dcd45
patch 9.0.1950: Vim9: error codes spread out
Christian Brabandt <cb@256bit.org>
parents:
33379
diff
changeset
|
153 *private-variable* *E1332* *E1333* |
33351
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
154 On the other hand, if you do not want the object variables to be read directly, |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
155 you can make them private. This is done by prefixing an underscore to the |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
156 name: > |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
157 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
158 this._lnum: number |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
159 this._col number |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
160 |
33351
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
161 Now you need to provide methods to get the value of the private variables. |
31885 | 162 These are commonly called getters. We recommend using a name that starts with |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
163 "Get": > |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
164 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
165 def GetLnum(): number |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
166 return this._lnum |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
167 enddef |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
168 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
169 def GetCol() number |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
170 return this._col |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
171 enddef |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
172 |
33351
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
173 This example isn't very useful, the variables might as well have been public. |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
174 It does become useful if you check the value. For example, restrict the line |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
175 number to the total number of lines: > |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
176 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
177 def GetLnum(): number |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
178 if this._lnum > this._lineCount |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
179 return this._lineCount |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
180 endif |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
181 return this._lnum |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
182 enddef |
33286
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
183 < |
33025
1d18c7fe609f
patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents:
32960
diff
changeset
|
184 Private methods ~ |
33383
5c12ca4dcd45
patch 9.0.1950: Vim9: error codes spread out
Christian Brabandt <cb@256bit.org>
parents:
33379
diff
changeset
|
185 *private-method* *E1366* |
33025
1d18c7fe609f
patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents:
32960
diff
changeset
|
186 If you want object methods to be accessible only from other methods of the |
1d18c7fe609f
patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents:
32960
diff
changeset
|
187 same class and not used from outside the class, then you can make them |
1d18c7fe609f
patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents:
32960
diff
changeset
|
188 private. This is done by prefixing the method name with an underscore: > |
1d18c7fe609f
patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents:
32960
diff
changeset
|
189 |
1d18c7fe609f
patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents:
32960
diff
changeset
|
190 class SomeClass |
1d18c7fe609f
patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents:
32960
diff
changeset
|
191 def _Foo(): number |
1d18c7fe609f
patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents:
32960
diff
changeset
|
192 return 10 |
1d18c7fe609f
patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents:
32960
diff
changeset
|
193 enddef |
1d18c7fe609f
patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents:
32960
diff
changeset
|
194 def Bar(): number |
1d18c7fe609f
patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents:
32960
diff
changeset
|
195 return this._Foo() |
1d18c7fe609f
patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents:
32960
diff
changeset
|
196 enddef |
1d18c7fe609f
patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents:
32960
diff
changeset
|
197 endclass |
1d18c7fe609f
patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents:
32960
diff
changeset
|
198 < |
1d18c7fe609f
patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents:
32960
diff
changeset
|
199 Accessing a private method outside the class will result in an error (using |
1d18c7fe609f
patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents:
32960
diff
changeset
|
200 the above class): > |
1d18c7fe609f
patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents:
32960
diff
changeset
|
201 |
1d18c7fe609f
patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents:
32960
diff
changeset
|
202 var a = SomeClass.new() |
1d18c7fe609f
patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents:
32960
diff
changeset
|
203 a._Foo() |
1d18c7fe609f
patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents:
32960
diff
changeset
|
204 < |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
205 Simplifying the new() method ~ |
33383
5c12ca4dcd45
patch 9.0.1950: Vim9: error codes spread out
Christian Brabandt <cb@256bit.org>
parents:
33379
diff
changeset
|
206 *new()* *constructor* |
33351
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
207 Many constructors take values for the object variables. Thus you very often |
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
208 see this pattern: > |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
209 |
31885 | 210 class SomeClass |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
211 this.lnum: number |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
212 this.col: number |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
213 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
214 def new(lnum: number, col: number) |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
215 this.lnum = lnum |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
216 this.col = col |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
217 enddef |
31885 | 218 endclass |
33379
7c9124711f99
patch 9.0.1948: Vim9: object variable "this." should only be used in constructor
Christian Brabandt <cb@256bit.org>
parents:
33372
diff
changeset
|
219 < |
7c9124711f99
patch 9.0.1948: Vim9: object variable "this." should only be used in constructor
Christian Brabandt <cb@256bit.org>
parents:
33372
diff
changeset
|
220 *E1390* |
33351
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
221 Not only is this text you need to write, it also has the type of each |
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
222 variables twice. Since this is so common a shorter way to write new() is |
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
223 provided: > |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
224 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
225 def new(this.lnum, this.col) |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
226 enddef |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
227 |
33351
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
228 The semantics are easy to understand: Providing the object variable name, |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
229 including "this.", as the argument to new() means the value provided in the |
33351
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
230 new() call is assigned to that object variable. This mechanism comes from the |
31885 | 231 Dart language. |
232 | |
33351
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
233 Putting together this way of using new() and making the variables public |
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
234 results in a much shorter class definition than what we started with: > |
31885 | 235 |
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 | |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
248 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
249 The sequence of constructing a new object is: |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
250 1. Memory is allocated and cleared. All values are zero/false/empty. |
33351
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
251 2. For each declared object variable that has an initializer, the expression |
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
252 is evaluated and assigned to the variable. This happens in the sequence |
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
253 the variables are declared in the class. |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
254 3. Arguments in the new() method in the "this.name" form are assigned. |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
255 4. The body of the new() method is executed. |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
256 |
31885 | 257 If the class extends a parent class, the same thing happens. In the second |
33351
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
258 step the object variables of the parent class are initialized first. There is |
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
259 no need to call "super()" or "new()" on the parent. |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
260 |
33385
b5ad84fdc702
patch 9.0.1951: Vim9: hard to debug vim9_class errors from CI
Christian Brabandt <cb@256bit.org>
parents:
33383
diff
changeset
|
261 *E1365* |
32903
54c01bb98b8e
patch 9.0.1760: vim9 class problem with new() constructor
Christian Brabandt <cb@256bit.org>
parents:
32294
diff
changeset
|
262 When defining the new() method the return type should not be specified. It |
54c01bb98b8e
patch 9.0.1760: vim9 class problem with new() constructor
Christian Brabandt <cb@256bit.org>
parents:
32294
diff
changeset
|
263 always returns an object of the class. |
54c01bb98b8e
patch 9.0.1760: vim9 class problem with new() constructor
Christian Brabandt <cb@256bit.org>
parents:
32294
diff
changeset
|
264 |
33385
b5ad84fdc702
patch 9.0.1951: Vim9: hard to debug vim9_class errors from CI
Christian Brabandt <cb@256bit.org>
parents:
33383
diff
changeset
|
265 *E1386* |
b5ad84fdc702
patch 9.0.1951: Vim9: hard to debug vim9_class errors from CI
Christian Brabandt <cb@256bit.org>
parents:
33383
diff
changeset
|
266 When invoking an object method, the method name should be preceded by the |
b5ad84fdc702
patch 9.0.1951: Vim9: hard to debug vim9_class errors from CI
Christian Brabandt <cb@256bit.org>
parents:
33383
diff
changeset
|
267 object variable name. A object method cannot be invoked using the class |
b5ad84fdc702
patch 9.0.1951: Vim9: hard to debug vim9_class errors from CI
Christian Brabandt <cb@256bit.org>
parents:
33383
diff
changeset
|
268 name. |
b5ad84fdc702
patch 9.0.1951: Vim9: hard to debug vim9_class errors from CI
Christian Brabandt <cb@256bit.org>
parents:
33383
diff
changeset
|
269 |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
270 ============================================================================== |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
271 |
33286
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
272 3. Class Variables and Methods *Vim9-class-member* |
31885 | 273 |
33286
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
274 *:static* *E1337* *E1338* *E1368* |
31885 | 275 Class members are declared with "static". They are used by the name without a |
33286
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
276 prefix in the class where they are defined: > |
31885 | 277 |
278 class OtherThing | |
279 this.size: number | |
280 static totalSize: number | |
281 | |
282 def new(this.size) | |
283 totalSize += this.size | |
284 enddef | |
285 endclass | |
286 < *E1340* *E1341* | |
33351
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
287 Since the name is used as-is, shadowing the name by a method argument name |
31885 | 288 or local variable name is not allowed. |
289 | |
33385
b5ad84fdc702
patch 9.0.1951: Vim9: hard to debug vim9_class errors from CI
Christian Brabandt <cb@256bit.org>
parents:
33383
diff
changeset
|
290 *E1374* *E1375* *E1384* *E1385* |
33286
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
291 To access a class member outside of the class where it is defined, the class |
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
292 name prefix must be used. A class member cannot be accessed using an object. |
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
293 |
31885 | 294 Just like object members the access can be made private by using an underscore |
295 as the first character in the name, and it can be made public by prefixing | |
296 "public": > | |
297 | |
298 class OtherThing | |
299 static total: number # anybody can read, only class can write | |
300 static _sum: number # only class can read and write | |
301 public static result: number # anybody can read and write | |
302 endclass | |
303 < | |
33286
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
304 *class-method* |
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
305 Class methods are also declared with "static". They can use the class |
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
306 variables but they have no access to the object variables, they cannot use the |
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
307 "this" keyword. |
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
308 > |
31885 | 309 class OtherThing |
310 this.size: number | |
311 static totalSize: number | |
312 | |
313 # Clear the total size and return the value it had before. | |
314 static def ClearTotalSize(): number | |
315 var prev = totalSize | |
316 totalSize = 0 | |
317 return prev | |
318 enddef | |
319 endclass | |
320 | |
33286
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
321 Inside the class the class method can be called by name directly, outside the |
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
322 class the class name must be prefixed: `OtherThing.ClearTotalSize()`. To use |
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
323 a super class method in a child class, the class name must be prefixed. |
31885 | 324 |
33025
1d18c7fe609f
patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents:
32960
diff
changeset
|
325 Just like object methods the access can be made private by using an underscore |
1d18c7fe609f
patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents:
32960
diff
changeset
|
326 as the first character in the method name: > |
1d18c7fe609f
patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents:
32960
diff
changeset
|
327 |
1d18c7fe609f
patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents:
32960
diff
changeset
|
328 class OtherThing |
1d18c7fe609f
patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents:
32960
diff
changeset
|
329 static def _Foo() |
1d18c7fe609f
patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents:
32960
diff
changeset
|
330 echo "Foo" |
1d18c7fe609f
patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents:
32960
diff
changeset
|
331 enddef |
1d18c7fe609f
patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents:
32960
diff
changeset
|
332 def Bar() |
33286
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
333 _Foo() |
33025
1d18c7fe609f
patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents:
32960
diff
changeset
|
334 enddef |
1d18c7fe609f
patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents:
32960
diff
changeset
|
335 endclass |
33068
d42927c6e556
patch 9.0.1821: Vim9 constructors are always static
Christian Brabandt <cb@256bit.org>
parents:
33025
diff
changeset
|
336 < |
d42927c6e556
patch 9.0.1821: Vim9 constructors are always static
Christian Brabandt <cb@256bit.org>
parents:
33025
diff
changeset
|
337 *E1370* |
d42927c6e556
patch 9.0.1821: Vim9 constructors are always static
Christian Brabandt <cb@256bit.org>
parents:
33025
diff
changeset
|
338 Note that constructors cannot be declared as "static", because they always |
d42927c6e556
patch 9.0.1821: Vim9 constructors are always static
Christian Brabandt <cb@256bit.org>
parents:
33025
diff
changeset
|
339 are. |
33025
1d18c7fe609f
patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents:
32960
diff
changeset
|
340 |
33286
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
341 To access the class methods and class variables of a super class in an |
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
342 extended class, the class name prefix should be used just as from anywhere |
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
343 outside of the defining class: > |
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
344 |
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
345 vim9script |
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
346 class Vehicle |
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
347 static nextID: number = 1000 |
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
348 static def GetID(): number |
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
349 nextID += 1 |
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
350 return nextID |
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
351 enddef |
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
352 endclass |
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
353 class Car extends Vehicle |
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
354 this.myID: number |
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
355 def new() |
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
356 this.myID = Vehicle.GetID() |
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
357 enddef |
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
358 endclass |
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
359 < |
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
360 Class variables and methods are not inherited by a child class. A child class |
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
361 can declare a static variable or a method with the same name as the one in the |
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
362 super class. Depending on the class where the member is used the |
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
363 corresponding class member will be used. The type of the class member in a |
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
364 child class can be different from that in the super class. |
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
365 |
31885 | 366 ============================================================================== |
367 | |
368 4. Using an abstract class *Vim9-abstract-class* | |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
369 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
370 An abstract class forms the base for at least one sub-class. In the class |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
371 model one often finds that a few classes have the same properties that can be |
31885 | 372 shared, but a class with these properties does not have enough state to create |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
373 an object from. A sub-class must extend the abstract class and add the |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
374 missing state and/or methods before it can be used to create objects for. |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
375 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
376 For example, a Shape class could store a color and thickness. You cannot |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
377 create a Shape object, it is missing the information about what kind of shape |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
378 it is. The Shape class functions as the base for a Square and a Triangle |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
379 class, for which objects can be created. Example: > |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
380 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
381 abstract class Shape |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
382 this.color = Color.Black |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
383 this.thickness = 10 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
384 endclass |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
385 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
386 class Square extends Shape |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
387 this.size: number |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
388 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
389 def new(this.size) |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
390 enddef |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
391 endclass |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
392 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
393 class Triangle extends Shape |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
394 this.base: number |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
395 this.height: number |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
396 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
397 def new(this.base, this.height) |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
398 enddef |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
399 endclass |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
400 < |
31885 | 401 An abstract class is defined the same way as a normal class, except that it |
402 does not have any new() method. *E1359* | |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
403 |
33286
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
404 *abstract-method* *E1371* *E1372* |
33217
499ba27ba0f6
patch 9.0.1885: Vim9: no support for abstract methods
Christian Brabandt <cb@256bit.org>
parents:
33068
diff
changeset
|
405 An abstract method can be defined in an abstract class by using the "abstract" |
33351
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
406 prefix when defining the method: > |
33217
499ba27ba0f6
patch 9.0.1885: Vim9: no support for abstract methods
Christian Brabandt <cb@256bit.org>
parents:
33068
diff
changeset
|
407 |
499ba27ba0f6
patch 9.0.1885: Vim9: no support for abstract methods
Christian Brabandt <cb@256bit.org>
parents:
33068
diff
changeset
|
408 abstract class Shape |
499ba27ba0f6
patch 9.0.1885: Vim9: no support for abstract methods
Christian Brabandt <cb@256bit.org>
parents:
33068
diff
changeset
|
409 abstract def Draw() |
33286
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
410 abstract static def SetColor() |
33217
499ba27ba0f6
patch 9.0.1885: Vim9: no support for abstract methods
Christian Brabandt <cb@256bit.org>
parents:
33068
diff
changeset
|
411 endclass |
33286
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
412 < |
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
413 *E1373* |
33217
499ba27ba0f6
patch 9.0.1885: Vim9: no support for abstract methods
Christian Brabandt <cb@256bit.org>
parents:
33068
diff
changeset
|
414 A class extending the abstract class must implement all the abstract methods. |
33286
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
415 The signature (arguments, argument types and return type) must be exactly the |
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
416 same. Class methods in an abstract class can also be abstract methods. |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
417 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
418 ============================================================================== |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
419 |
31885 | 420 5. Using an interface *Vim9-using-interface* |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
421 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
422 The example above with Shape, Square and Triangle can be made more useful if |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
423 we add a method to compute the surface of the object. For that we create the |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
424 interface called HasSurface, which specifies one method Surface() that returns |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
425 a number. This example extends the one above: > |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
426 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
427 abstract class Shape |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
428 this.color = Color.Black |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
429 this.thickness = 10 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
430 endclass |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
431 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
432 interface HasSurface |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
433 def Surface(): number |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
434 endinterface |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
435 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
436 class Square extends Shape implements HasSurface |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
437 this.size: number |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
438 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
439 def new(this.size) |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
440 enddef |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
441 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
442 def Surface(): number |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
443 return this.size * this.size |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
444 enddef |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
445 endclass |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
446 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
447 class Triangle extends Shape implements HasSurface |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
448 this.base: number |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
449 this.height: number |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
450 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
451 def new(this.base, this.height) |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
452 enddef |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
453 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
454 def Surface(): number |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
455 return this.base * this.height / 2 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
456 enddef |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
457 endclass |
33286
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
458 < |
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
459 *E1348* *E1349* *E1367* *E1382* *E1383* |
31671 | 460 If a class declares to implement an interface, all the items specified in the |
33286
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
461 interface must appear in the class, with the same types. |
31671 | 462 |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
463 The interface name can be used as a type: > |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
464 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
465 var shapes: list<HasSurface> = [ |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
466 Square.new(12), |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
467 Triangle.new(8, 15), |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
468 ] |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
469 for shape in shapes |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
470 echo $'the surface is {shape.Surface()}' |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
471 endfor |
33286
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
472 < |
33372
3e9a91624b40
patch 9.0.1945: Vim9: missing support for ro-vars in interface
Christian Brabandt <cb@256bit.org>
parents:
33351
diff
changeset
|
473 *E1378* *E1379* *E1380* *E1387* |
3e9a91624b40
patch 9.0.1945: Vim9: missing support for ro-vars in interface
Christian Brabandt <cb@256bit.org>
parents:
33351
diff
changeset
|
474 An interface can contain only object methods and read-only object variables. |
3e9a91624b40
patch 9.0.1945: Vim9: missing support for ro-vars in interface
Christian Brabandt <cb@256bit.org>
parents:
33351
diff
changeset
|
475 An interface cannot contain read-write and private object variables, private |
3e9a91624b40
patch 9.0.1945: Vim9: missing support for ro-vars in interface
Christian Brabandt <cb@256bit.org>
parents:
33351
diff
changeset
|
476 object methods, class variables and class methods. |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
477 |
33286
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
478 An interface can extend another interface using "extends". The sub-interface |
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
479 inherits all the instance variables and methods from the super interface. |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
480 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
481 ============================================================================== |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
482 |
31885 | 483 6. More class details *Vim9-class* *Class* *class* |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
484 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
485 Defining a class ~ |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
486 *:class* *:endclass* *:abstract* |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
487 A class is defined between `:class` and `:endclass`. The whole class is |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
488 defined in one script file. It is not possible to add to a class later. |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
489 |
31430 | 490 A class can only be defined in a |Vim9| script file. *E1316* |
31396
307f68a41b03
patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
31335
diff
changeset
|
491 A class cannot be defined inside a function. |
307f68a41b03
patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
31335
diff
changeset
|
492 |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
493 It is possible to define more than one class in a script file. Although it |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
494 usually is better to export only one main class. It can be useful to define |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
495 types, enums and helper classes though. |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
496 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
497 The `:abstract` keyword may be prefixed and `:export` may be used. That gives |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
498 these variants: > |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
499 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
500 class ClassName |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
501 endclass |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
502 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
503 export class ClassName |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
504 endclass |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
505 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
506 abstract class ClassName |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
507 endclass |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
508 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
509 export abstract class ClassName |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
510 endclass |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
511 < |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
512 *E1314* |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
513 The class name should be CamelCased. It must start with an uppercase letter. |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
514 That avoids clashing with builtin types. |
31430 | 515 *E1315* |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
516 After the class name these optional items can be used. Each can appear only |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
517 once. They can appear in any order, although this order is recommended: > |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
518 extends ClassName |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
519 implements InterfaceName, OtherInterface |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
520 specifies SomeInterface |
33286
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
521 < *E1355* *E1369* |
33351
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
522 Each variable and method name can be used only once. It is not possible to |
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
523 define a method with the same name and different type of arguments. It is not |
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
524 possible to use a public and private member variable with the same name. A |
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
525 object variable name used in a super class cannot be reused in a child class. |
31885 | 526 |
527 | |
33351
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
528 Object Variable Initialization ~ |
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
529 If the type of a variable is not explicitly specified in a class, then it is |
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
530 set to "any" during class definition. When an object is instantiated from the |
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
531 class, then the type of the variable is set. |
32960
d5c05e15cf81
patch 9.0.1780: Vim9 type not defined during object creation
Christian Brabandt <cb@256bit.org>
parents:
32903
diff
changeset
|
532 |
33401
bb99820510ef
patch 9.0.1959: Vim9: methods parameters and types are covariant
Christian Brabandt <cb@256bit.org>
parents:
33385
diff
changeset
|
533 The following reserved keyword names cannot be used as an object or class |
bb99820510ef
patch 9.0.1959: Vim9: methods parameters and types are covariant
Christian Brabandt <cb@256bit.org>
parents:
33385
diff
changeset
|
534 variable name: "super", "this", "true", "false", "null", "null_blob", |
bb99820510ef
patch 9.0.1959: Vim9: methods parameters and types are covariant
Christian Brabandt <cb@256bit.org>
parents:
33385
diff
changeset
|
535 "null_dict", "null_function", "null_list", "null_partial", "null_string", |
bb99820510ef
patch 9.0.1959: Vim9: methods parameters and types are covariant
Christian Brabandt <cb@256bit.org>
parents:
33385
diff
changeset
|
536 "null_channel" and "null_job". |
32960
d5c05e15cf81
patch 9.0.1780: Vim9 type not defined during object creation
Christian Brabandt <cb@256bit.org>
parents:
32903
diff
changeset
|
537 |
31885 | 538 Extending a class ~ |
539 *extends* | |
31671 | 540 A class can extend one other class. *E1352* *E1353* *E1354* |
31885 | 541 The basic idea is to build on top of an existing class, add properties to it. |
542 | |
543 The extended class is called the "base class" or "super class". The new class | |
544 is called the "child class". | |
545 | |
33351
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
546 Object variables from the base class are all taken over by the child class. It |
31885 | 547 is not possible to override them (unlike some other languages). |
548 | |
549 *E1356* *E1357* *E1358* | |
33401
bb99820510ef
patch 9.0.1959: Vim9: methods parameters and types are covariant
Christian Brabandt <cb@256bit.org>
parents:
33385
diff
changeset
|
550 Object methods of the base class can be overruled. The number of arguments |
bb99820510ef
patch 9.0.1959: Vim9: methods parameters and types are covariant
Christian Brabandt <cb@256bit.org>
parents:
33385
diff
changeset
|
551 must be exactly the same. The method argument type can be a contra-variant |
bb99820510ef
patch 9.0.1959: Vim9: methods parameters and types are covariant
Christian Brabandt <cb@256bit.org>
parents:
33385
diff
changeset
|
552 type of the base class method argument type. The method return value type can |
bb99820510ef
patch 9.0.1959: Vim9: methods parameters and types are covariant
Christian Brabandt <cb@256bit.org>
parents:
33385
diff
changeset
|
553 be a covariant type of the base class method return value type. The method of |
bb99820510ef
patch 9.0.1959: Vim9: methods parameters and types are covariant
Christian Brabandt <cb@256bit.org>
parents:
33385
diff
changeset
|
554 the base class can be called by prefixing "super.". |
31885 | 555 |
33286
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
556 *E1377* |
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
557 The access level of a method (public or private) in a child class should be |
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
558 the same as the super class. |
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
559 |
31885 | 560 Other object methods of the base class are taken over by the child class. |
561 | |
33351
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
562 Class methods, including methods starting with "new", can be overruled, like |
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
563 with object methods. The method on the base class can be called by prefixing |
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
564 the name of the class (for class methods) or "super.". |
31885 | 565 |
566 Unlike other languages, the constructor of the base class does not need to be | |
567 invoked. In fact, it cannot be invoked. If some initialization from the base | |
568 class also needs to be done in a child class, put it in an object method and | |
569 call that method from every constructor(). | |
570 | |
33351
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
571 If the base class did not specify a new() method then one was automatically |
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
572 created. This method will not be taken over by the child class. The child |
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
573 class can define its own new() method, or, if there isn't one, a new() method |
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
574 will be added automatically. |
31885 | 575 |
576 | |
577 A class implementing an interface ~ | |
33372
3e9a91624b40
patch 9.0.1945: Vim9: missing support for ro-vars in interface
Christian Brabandt <cb@256bit.org>
parents:
33351
diff
changeset
|
578 *implements* *E1346* *E1347* *E1389* |
31671 | 579 A class can implement one or more interfaces. The "implements" keyword can |
580 only appear once *E1350* . Multiple interfaces can be specified, separated by | |
581 commas. Each interface name can appear only once. *E1351* | |
31885 | 582 |
583 | |
584 A class defining an interface ~ | |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
585 *specifies* |
33351
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
586 A class can declare its interface, the object variables and methods, with a |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
587 named interface. This avoids the need for separately specifying the |
31396
307f68a41b03
patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
31335
diff
changeset
|
588 interface, which is often done in many languages, especially Java. |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
589 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
590 |
31430 | 591 Items in a class ~ |
33372
3e9a91624b40
patch 9.0.1945: Vim9: missing support for ro-vars in interface
Christian Brabandt <cb@256bit.org>
parents:
33351
diff
changeset
|
592 *E1318* *E1325* *E1388* |
32061 | 593 Inside a class, in between `:class` and `:endclass`, these items can appear: |
33351
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
594 - An object variable declaration: > |
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
595 this._privateVariableName: memberType |
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
596 this.readonlyVariableName: memberType |
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
597 public this.readwriteVariableName: memberType |
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
598 - A class variable declaration: > |
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
599 static _privateClassVariableName: memberType |
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
600 static readonlyClassVariableName: memberType |
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
601 static public readwriteClassVariableName: memberType |
31430 | 602 - A constructor method: > |
32004 | 603 def new(arguments) |
604 def newName(arguments) | |
33286
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
605 - A class method: > |
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
606 static def SomeMethod(arguments) |
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
607 static def _PrivateMethod(arguments) |
31430 | 608 - An object method: > |
32004 | 609 def SomeMethod(arguments) |
33286
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
610 def _PrivateMethod(arguments) |
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
611 |
33351
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
612 For the object variable the type must be specified. The best way is to do |
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
613 this explicitly with ": {type}". For simple types you can also use an |
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
614 initializer, such as "= 123", and Vim will see that the type is a number. |
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
615 Avoid doing this for more complex types and when the type will be incomplete. |
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
616 For example: > |
31579 | 617 this.nameList = [] |
618 This specifies a list, but the item type is unknown. Better use: > | |
619 this.nameList: list<string> | |
620 The initialization isn't needed, the list is empty by default. | |
621 *E1330* | |
622 Some types cannot be used, such as "void", "null" and "v:none". | |
31430 | 623 |
624 | |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
625 Defining an interface ~ |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
626 *:interface* *:endinterface* |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
627 An interface is defined between `:interface` and `:endinterface`. It may be |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
628 prefixed with `:export`: > |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
629 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
630 interface InterfaceName |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
631 endinterface |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
632 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
633 export interface InterfaceName |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
634 endinterface |
31671 | 635 < *E1344* |
33351
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
636 An interface can declare object variables, just like in a class but without |
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
637 any initializer. |
31671 | 638 *E1345* |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
639 An interface can declare methods with `:def`, including the arguments and |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
640 return type, but without the body and without `:enddef`. Example: > |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
641 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
642 interface HasSurface |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
643 this.size: number |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
644 def Surface(): number |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
645 endinterface |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
646 |
31671 | 647 An interface name must start with an uppercase letter. *E1343* |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
648 The "Has" prefix can be used to make it easier to guess this is an interface |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
649 name, with a hint about what it provides. |
31671 | 650 An interface can only be defined in a |Vim9| script file. *E1342* |
33286
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
651 An interface cannot "implement" another interface but it can "extend" another |
0c3553cfe22e
patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents:
33217
diff
changeset
|
652 interface. *E1381* |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
653 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
654 |
32004 | 655 null object ~ |
656 | |
32061 | 657 When a variable is declared to have the type of an object, but it is not |
32004 | 658 initialized, the value is null. When trying to use this null object Vim often |
659 does not know what class was supposed to be used. Vim then cannot check if | |
33351
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
660 a variable name is correct and you will get an "Using a null object" error, |
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
661 even when the variable name is invalid. *E1360* *E1362* *E1363* |
32004 | 662 |
663 | |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
664 Default constructor ~ |
33383
5c12ca4dcd45
patch 9.0.1950: Vim9: error codes spread out
Christian Brabandt <cb@256bit.org>
parents:
33379
diff
changeset
|
665 *default-constructor* |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
666 In case you define a class without a new() method, one will be automatically |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
667 defined. This default constructor will have arguments for all the object |
33351
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
668 variables, in the order they were specified. Thus if your class looks like: > |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
669 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
670 class AutoNew |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
671 this.name: string |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
672 this.age: number |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
673 this.gender: Gender |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
674 endclass |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
675 |
33351
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
676 Then the default constructor will be: > |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
677 |
31441
e572ff386670
patch 9.0.1053: default constructor arguments are not optional
Bram Moolenaar <Bram@vim.org>
parents:
31430
diff
changeset
|
678 def new(this.name = v:none, this.age = v:none, this.gender = v:none) |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
679 enddef |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
680 |
31441
e572ff386670
patch 9.0.1053: default constructor arguments are not optional
Bram Moolenaar <Bram@vim.org>
parents:
31430
diff
changeset
|
681 The "= v:none" default values make the arguments optional. Thus you can also |
e572ff386670
patch 9.0.1053: default constructor arguments are not optional
Bram Moolenaar <Bram@vim.org>
parents:
31430
diff
changeset
|
682 call `new()` without any arguments. No assignment will happen and the default |
33351
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
683 value for the object variables will be used. This is a more useful example, |
31441
e572ff386670
patch 9.0.1053: default constructor arguments are not optional
Bram Moolenaar <Bram@vim.org>
parents:
31430
diff
changeset
|
684 with default values: > |
31430 | 685 |
686 class TextPosition | |
687 this.lnum: number = 1 | |
688 this.col: number = 1 | |
689 endclass | |
690 | |
691 If you want the constructor to have mandatory arguments, you need to write it | |
692 yourself. For example, if for the AutoNew class above you insist on getting | |
693 the name, you can define the constructor like this: > | |
694 | |
31441
e572ff386670
patch 9.0.1053: default constructor arguments are not optional
Bram Moolenaar <Bram@vim.org>
parents:
31430
diff
changeset
|
695 def new(this.name, this.age = v:none, this.gender = v:none) |
31430 | 696 enddef |
31441
e572ff386670
patch 9.0.1053: default constructor arguments are not optional
Bram Moolenaar <Bram@vim.org>
parents:
31430
diff
changeset
|
697 < *E1328* |
e572ff386670
patch 9.0.1053: default constructor arguments are not optional
Bram Moolenaar <Bram@vim.org>
parents:
31430
diff
changeset
|
698 Note that you cannot use another default value than "v:none" here. If you |
33351
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
699 want to initialize the object variables, do it where they are declared. This |
31441
e572ff386670
patch 9.0.1053: default constructor arguments are not optional
Bram Moolenaar <Bram@vim.org>
parents:
31430
diff
changeset
|
700 way you only need to look in one place for the default values. |
31430 | 701 |
33351
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
702 All object variables will be used in the default constructor, also private |
31671 | 703 access ones. |
704 | |
33351
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
705 If the class extends another one, the object variables of that class will come |
31671 | 706 first. |
707 | |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
708 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
709 Multiple constructors ~ |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
710 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
711 Normally a class has just one new() constructor. In case you find that the |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
712 constructor is often called with the same arguments you may want to simplify |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
713 your code by putting those arguments into a second constructor method. For |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
714 example, if you tend to use the color black a lot: > |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
715 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
716 def new(this.garment, this.color, this.size) |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
717 enddef |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
718 ... |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
719 var pants = new(Garment.pants, Color.black, "XL") |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
720 var shirt = new(Garment.shirt, Color.black, "XL") |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
721 var shoes = new(Garment.shoes, Color.black, "45") |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
722 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
723 Instead of repeating the color every time you can add a constructor that |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
724 includes it: > |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
725 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
726 def newBlack(this.garment, this.size) |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
727 this.color = Color.black |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
728 enddef |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
729 ... |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
730 var pants = newBlack(Garment.pants, "XL") |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
731 var shirt = newBlack(Garment.shirt, "XL") |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
732 var shoes = newBlack(Garment.shoes, "9.5") |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
733 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
734 Note that the method name must start with "new". If there is no method called |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
735 "new()" then the default constructor is added, even though there are other |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
736 constructor methods. |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
737 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
738 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
739 ============================================================================== |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
740 |
31885 | 741 7. Type definition *Vim9-type* *:type* |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
742 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
743 A type definition is giving a name to a type specification. For Example: > |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
744 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
745 :type ListOfStrings list<string> |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
746 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
747 TODO: more explanation |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
748 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
749 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
750 ============================================================================== |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
751 |
31885 | 752 8. Enum *Vim9-enum* *:enum* *:endenum* |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
753 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
754 An enum is a type that can have one of a list of values. Example: > |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
755 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
756 :enum Color |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
757 White |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
758 Red |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
759 Green |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
760 Blue |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
761 Black |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
762 :endenum |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
763 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
764 TODO: more explanation |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
765 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
766 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
767 ============================================================================== |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
768 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
769 9. Rationale |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
770 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
771 Most of the choices for |Vim9| classes come from popular and recently |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
772 developed languages, such as Java, TypeScript and Dart. The syntax has been |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
773 made to fit with the way Vim script works, such as using `endclass` instead of |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
774 using curly braces around the whole class. |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
775 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
776 Some common constructs of object-oriented languages were chosen very long ago |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
777 when this kind of programming was still new, and later found to be |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
778 sub-optimal. By this time those constructs were widely used and changing them |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
779 was not an option. In Vim we do have the freedom to make different choices, |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
780 since classes are completely new. We can make the syntax simpler and more |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
781 consistent than what "old" languages use. Without diverting too much, it |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
782 should still mostly look like what you know from existing languages. |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
783 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
784 Some recently developed languages add all kinds of fancy features that we |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
785 don't need for Vim. But some have nice ideas that we do want to use. |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
786 Thus we end up with a base of what is common in popular languages, dropping |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
787 what looks like a bad idea, and adding some nice features that are easy to |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
788 understand. |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
789 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
790 The main rules we use to make decisions: |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
791 - Keep it simple. |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
792 - No surprises, mostly do what other languages are doing. |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
793 - Avoid mistakes from the past. |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
794 - Avoid the need for the script writer to consult the help to understand how |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
795 things work, most things should be obvious. |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
796 - Keep it consistent. |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
797 - Aim at an average size plugin, not at a huge project. |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
798 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
799 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
800 Using new() for the constructor ~ |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
801 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
802 Many languages use the class name for the constructor method. A disadvantage |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
803 is that quite often this is a long name. And when changing the class name all |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
804 constructor methods need to be renamed. Not a big deal, but still a |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
805 disadvantage. |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
806 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
807 Other languages, such as TypeScript, use a specific name, such as |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
808 "constructor()". That seems better. However, using "new" or "new()" to |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
809 create a new object has no obvious relation with "constructor()". |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
810 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
811 For |Vim9| script using the same method name for all constructors seemed like |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
812 the right choice, and by calling it new() the relation between the caller and |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
813 the method being called is obvious. |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
814 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
815 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
816 No overloading of the constructor ~ |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
817 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
818 In Vim script, both legacy and |Vim9| script, there is no overloading of |
33351
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
819 methods. That means it is not possible to use the same method name with |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
820 different types of arguments. Therefore there also is only one new() |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
821 constructor. |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
822 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
823 With |Vim9| script it would be possible to support overloading, since |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
824 arguments are typed. However, this gets complicated very quickly. Looking at |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
825 a new() call one has to inspect the types of the arguments to know which of |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
826 several new() methods is actually being called. And that can require |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
827 inspecting quite a bit of code. For example, if one of the arguments is the |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
828 return value of a method, you need to find that method to see what type it is |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
829 returning. |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
830 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
831 Instead, every constructor has to have a different name, starting with "new". |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
832 That way multiple constructors with different arguments are possible, while it |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
833 is very easy to see which constructor is being used. And the type of |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
834 arguments can be properly checked. |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
835 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
836 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
837 No overloading of methods ~ |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
838 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
839 Same reasoning as for the constructor: It is often not obvious what type |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
840 arguments have, which would make it difficult to figure out what method is |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
841 actually being called. Better just give the methods a different name, then |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
842 type checking will make sure it works as you intended. This rules out |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
843 polymorphism, which we don't really need anyway. |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
844 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
845 |
31671 | 846 Single inheritance and interfaces ~ |
847 | |
848 Some languages support multiple inheritance. Although that can be useful in | |
849 some cases, it makes the rules of how a class works quite complicated. | |
850 Instead, using interfaces to declare what is supported is much simpler. The | |
851 very popular Java language does it this way, and it should be good enough for | |
31885 | 852 Vim. The "keep it simple" rule applies here. |
31671 | 853 |
854 Explicitly declaring that a class supports an interface makes it easy to see | |
855 what a class is intended for. It also makes it possible to do proper type | |
856 checking. When an interface is changed any class that declares to implement | |
857 it will be checked if that change was also changed. The mechanism to assume a | |
858 class implements an interface just because the methods happen to match is | |
859 brittle and leads to obscure problems, let's not do that. | |
860 | |
861 | |
33351
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
862 Using "this.variable" everywhere ~ |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
863 |
33351
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
864 The object variables in various programming languages can often be accessed in |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
865 different ways, depending on the location. Sometimes "this." has to be |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
866 prepended to avoid ambiguity. They are usually declared without "this.". |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
867 That is quite inconsistent and sometimes confusing. |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
868 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
869 A very common issue is that in the constructor the arguments use the same name |
33351
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
870 as the object variable. Then for these variables "this." needs to be prefixed |
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
871 in the body, while for other variables this is not needed and often omitted. |
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
872 This leads to a mix of variables with and without "this.", which is |
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
873 inconsistent. |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
874 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
875 For |Vim9| classes the "this." prefix is always used. Also for declaring the |
33351
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
876 variables. Simple and consistent. When looking at the code inside a class |
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
877 it's also directly clear which variable references are object variables and |
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
878 which aren't. |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
879 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
880 |
33351
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
881 Using class variables ~ |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
882 |
33351
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
883 Using "static variable" to declare a class variable is very common, nothing |
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
884 new here. In |Vim9| script these can be accessed directly by their name. |
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
885 Very much like how a script-local variable can be used in a method. Since |
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
886 object variables are always accessed with "this." prepended, it's also quickly |
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
887 clear what kind of variable it is. |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
888 |
33351
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
889 TypeScript prepends the class name before the class variable name, also inside |
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
890 the class. This has two problems: The class name can be rather long, taking |
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
891 up quite a bit of space, and when the class is renamed all these places need |
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
892 to be changed too. |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
893 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
894 |
33351
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
895 Declaring object and class variables ~ |
31671 | 896 |
897 The main choice is whether to use "var" as with variable declarations. | |
898 TypeScript does not use it: > | |
899 class Point { | |
900 x: number; | |
901 y = 0; | |
902 } | |
903 | |
33351
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
904 Following that Vim object variables could be declared like this: > |
31671 | 905 class Point |
906 this.x: number | |
907 this.y = 0 | |
908 endclass | |
909 | |
910 Some users pointed out that this looks more like an assignment than a | |
911 declaration. Adding "var" changes that: > | |
912 class Point | |
913 var this.x: number | |
914 var this.y = 0 | |
915 endclass | |
916 | |
33351
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
917 We also need to be able to declare class variables using the "static" keyword. |
31671 | 918 There we can also choose to leave out "var": > |
919 class Point | |
920 var this.x: number | |
921 static count = 0 | |
922 endclass | |
923 | |
924 Or do use it, before "static": > | |
925 class Point | |
926 var this.x: number | |
927 var static count = 0 | |
928 endclass | |
929 | |
930 Or after "static": > | |
931 class Point | |
932 var this.x: number | |
933 static var count = 0 | |
934 endclass | |
935 | |
936 This is more in line with "static def Func()". | |
937 | |
938 There is no clear preference whether to use "var" or not. The two main | |
939 reasons to leave it out are: | |
940 1. TypeScript, Java and other popular languages do not use it. | |
941 2. Less clutter. | |
942 | |
943 | |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
944 Using "ClassName.new()" to construct an object ~ |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
945 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
946 Many languages use the "new" operator to create an object, which is actually |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
947 kind of strange, since the constructor is defined as a method with arguments, |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
948 not a command. TypeScript also has the "new" keyword, but the method is |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
949 called "constructor()", it is hard to see the relation between the two. |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
950 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
951 In |Vim9| script the constructor method is called new(), and it is invoked as |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
952 new(), simple and straightforward. Other languages use "new ClassName()", |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
953 while there is no ClassName() method, it's a method by another name in the |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
954 class called ClassName. Quite confusing. |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
955 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
956 |
33351
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
957 Default read access to object variables ~ |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
958 |
33351
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
959 Some users will remark that the access rules for object variables are |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
960 asymmetric. Well, that is intentional. Changing a value is a very different |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
961 action than reading a value. The read operation has no side effects, it can |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
962 be done any number of times without affecting the object. Changing the value |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
963 can have many side effects, and even have a ripple effect, affecting other |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
964 objects. |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
965 |
33351
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
966 When adding object variables one usually doesn't think much about this, just |
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
967 get the type right. And normally the values are set in the new() method. |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
968 Therefore defaulting to read access only "just works" in most cases. And when |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
969 directly writing you get an error, which makes you wonder if you actually want |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
970 to allow that. This helps writing code with fewer mistakes. |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
971 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
972 |
33351
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
973 Making object variables private with an underscore ~ |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
974 |
33351
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
975 When an object variable is private, it can only be read and changed inside the |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
976 class (and in sub-classes), then it cannot be used outside of the class. |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
977 Prepending an underscore is a simple way to make that visible. Various |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
978 programming languages have this as a recommendation. |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
979 |
33351
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
980 In case you change your mind and want to make the object variable accessible |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
981 outside of the class, you will have to remove the underscore everywhere. |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
982 Since the name only appears in the class (and sub-classes) they will be easy |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
983 to find and change. |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
984 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
985 The other way around is much harder: you can easily prepend an underscore to |
33351
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
986 the object variable inside the class to make it private, but any usage |
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
987 elsewhere you will have to track down and change. You may have to make it a |
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
988 "set" method call. This reflects the real world problem that taking away |
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
989 access requires work to be done for all places where that access exists. |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
990 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
991 An alternative would have been using the "private" keyword, just like "public" |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
992 changes the access in the other direction. Well, that's just to reduce the |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
993 number of keywords. |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
994 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
995 |
33351
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
996 No protected object variables ~ |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
997 |
33351
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
998 Some languages provide several ways to control access to object variables. |
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
999 The most known is "protected", and the meaning varies from language to |
a3bbd065fb87
runtime(doc): Vim9: Consistenly use class/object variable and class/object method in help (#13149)
Christian Brabandt <cb@256bit.org>
parents:
33286
diff
changeset
|
1000 language. Others are "shared", "private" and even "friend". |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1001 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1002 These rules make life more difficult. That can be justified in projects where |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1003 many people work on the same, complex code where it is easy to make mistakes. |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1004 Especially when refactoring or other changes to the class model. |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1005 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1006 The Vim scripts are expected to be used in a plugin, with just one person or a |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1007 small team working on it. Complex rules then only make it more complicated, |
32294 | 1008 the extra safety provided by the rules isn't really needed. Let's just keep |
1009 it simple and not specify access details. | |
31335
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1010 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1011 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1012 ============================================================================== |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1013 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1014 10. To be done later |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1015 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1016 Can a newSomething() constructor invoke another constructor? If yes, what are |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1017 the restrictions? |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1018 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1019 Thoughts: |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1020 - Generics for a class: `class <Tkey, Tentry>` |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1021 - Generics for a function: `def <Tkey> GetLast(key: Tkey)` |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1022 - Mixins: not sure if that is useful, leave out for simplicity. |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1023 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1024 Some things that look like good additions: |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1025 - For testing: Mock mechanism |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1026 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1027 An important class to be provided is "Promise". Since Vim is single |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1028 threaded, connecting asynchronous operations is a natural way of allowing |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1029 plugins to do their work without blocking the user. It's a uniform way to |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1030 invoke callbacks and handle timeouts and errors. |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1031 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1032 |
5acc0d2cf4f7
patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1033 vim:tw=78:ts=8:noet:ft=help:norl: |