1
- *vim9class.txt* For Vim version 9.1. Last change: 2024 Dec 29
1
+ *vim9class.txt* For Vim version 9.1. Last change: 2025 Feb 16
2
2
3
3
4
4
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -52,7 +52,6 @@ An interface is used to specify properties of an object:
52
52
The class hierarchy allows for single inheritance. Otherwise interfaces are
53
53
to be used where needed.
54
54
55
-
56
55
Class modeling ~
57
56
58
57
You can model classes any way you like. Keep in mind what you are building,
@@ -122,7 +121,6 @@ using the object name followed by a dot following by the member: >
122
121
A class name cannot be used as an expression. A class name cannot be used in
123
122
the left-hand-side of an assignment.
124
123
125
-
126
124
Object variable write access ~
127
125
*read-only-variable*
128
126
Now try to change an object variable directly: >
@@ -272,6 +270,9 @@ no need to call "super()" or "new()" on the parent.
272
270
When defining the new() method the return type should not be specified. It
273
271
always returns an object of the class.
274
272
273
+ The new() method can be made a protected method by using "_new()". This can
274
+ be used to support the singleton design pattern.
275
+
275
276
*E1386*
276
277
When invoking an object method, the method name should be preceded by the
277
278
object variable name. An object method cannot be invoked using the class
@@ -623,13 +624,15 @@ once. They can appear in any order, although this order is recommended: >
623
624
extends ClassName
624
625
implements InterfaceName, OtherInterface
625
626
specifies SomeInterface
626
- < *E1355* *E1369*
627
+ <
628
+ The "specifies" feature is currently not implemented.
629
+
630
+ *E1355* *E1369*
627
631
Each variable and method name can be used only once. It is not possible to
628
632
define a method with the same name and different type of arguments. It is not
629
633
possible to use a public and protected member variable with the same name. An
630
634
object variable name used in a super class cannot be reused in a child class.
631
635
632
-
633
636
Object Variable Initialization ~
634
637
635
638
If the type of a variable is not explicitly specified in a class, then it is
@@ -686,13 +689,12 @@ A class can implement one or more interfaces. The "implements" keyword can
686
689
only appear once *E1350* . Multiple interfaces can be specified, separated by
687
690
commas. Each interface name can appear only once. *E1351*
688
691
689
-
690
692
A class defining an interface ~
691
693
*specifies*
692
694
A class can declare its interface, the object variables and methods, with a
693
695
named interface. This avoids the need for separately specifying the
694
696
interface, which is often done in many languages, especially Java.
695
-
697
+ TODO: This is currently not implemented.
696
698
697
699
Items in a class ~
698
700
*E1318* *E1325* *E1388*
@@ -733,25 +735,25 @@ Some of the builtin functions like |empty()|, |len()| and |string()| can be
733
735
used with an object. An object can implement a method with the same name as
734
736
these builtin functions to return an object-specific value.
735
737
736
- *E1412*
738
+ *E1412*
737
739
The following builtin methods are supported:
738
- *object-empty()*
740
+ *object-empty()*
739
741
empty() Invoked by the | empty() | function to check whether an object is
740
742
empty. If this method is missing, then true is returned. This
741
743
method should not accept any arguments and must return a boolean.
742
- *object-len()*
744
+ *object-len()*
743
745
len() Invoked by the | len() | function to return the length of an
744
746
object. If this method is missing in the class, then an error is
745
747
given and zero is returned. This method should not accept any
746
748
arguments and must return a number.
747
- *object-string()*
749
+ *object-string()*
748
750
string() Invoked by the | string() | function to get a textual
749
751
representation of an object. Also used by the | :echo | command
750
752
for an object. If this method is missing in the class, then a
751
753
built-in default textual representation is used. This method
752
754
should not accept any arguments and must return a string.
753
755
754
- *E1413*
756
+ *E1413*
755
757
A class method cannot be used as a builtin method.
756
758
757
759
Defining an interface ~
@@ -783,7 +785,6 @@ An interface can only be defined in a |Vim9| script file. *E1342*
783
785
An interface cannot "implement" another interface but it can "extend" another
784
786
interface. *E1381*
785
787
786
-
787
788
null object ~
788
789
789
790
When a variable is declared to have the type of an object, but it is not
@@ -792,7 +793,6 @@ does not know what class was supposed to be used. Vim then cannot check if
792
793
a variable name is correct and you will get a "Using a null object" error,
793
794
even when the variable name is invalid. *E1360* *E1362*
794
795
795
-
796
796
Default constructor ~
797
797
*default-constructor*
798
798
In case you define a class without a new() method, one will be automatically
@@ -1115,7 +1115,6 @@ For |Vim9| script using the same method name for all constructors seemed like
1115
1115
the right choice, and by calling it new() the relation between the caller and
1116
1116
the method being called is obvious.
1117
1117
1118
-
1119
1118
No overloading of the constructor ~
1120
1119
1121
1120
In Vim script, both legacy and | Vim9 | script, there is no overloading of
@@ -1136,7 +1135,6 @@ That way multiple constructors with different arguments are possible, while it
1136
1135
is very easy to see which constructor is being used. And the type of
1137
1136
arguments can be properly checked.
1138
1137
1139
-
1140
1138
No overloading of methods ~
1141
1139
1142
1140
Same reasoning as for the constructor: It is often not obvious what type
@@ -1145,7 +1143,6 @@ actually being called. Better just give the methods a different name, then
1145
1143
type checking will make sure it works as you intended. This rules out
1146
1144
polymorphism, which we don't really need anyway.
1147
1145
1148
-
1149
1146
Single inheritance and interfaces ~
1150
1147
1151
1148
Some languages support multiple inheritance. Although that can be useful in
@@ -1161,7 +1158,6 @@ it will be checked if that change was also changed. The mechanism to assume a
1161
1158
class implements an interface just because the methods happen to match is
1162
1159
brittle and leads to obscure problems, let's not do that.
1163
1160
1164
-
1165
1161
Using "this.variable" everywhere ~
1166
1162
1167
1163
The object variables in various programming languages can often be accessed in
@@ -1180,7 +1176,6 @@ variables. Simple and consistent. When looking at the code inside a class
1180
1176
it's also directly clear which variable references are object variables and
1181
1177
which aren't.
1182
1178
1183
-
1184
1179
Using class variables ~
1185
1180
1186
1181
Using "static variable" to declare a class variable is very common, nothing
@@ -1194,7 +1189,6 @@ the class. This has two problems: The class name can be rather long, taking
1194
1189
up quite a bit of space, and when the class is renamed all these places need
1195
1190
to be changed too.
1196
1191
1197
-
1198
1192
Declaring object and class variables ~
1199
1193
1200
1194
The main choice is whether to use "var" as with variable declarations.
@@ -1248,7 +1242,6 @@ function declaration syntax for class/object variables and methods. Vim9 also
1248
1242
reuses the general function declaration syntax for methods. So, for the sake
1249
1243
of consistency, we require "var" in these declarations.
1250
1244
1251
-
1252
1245
Using "ClassName.new()" to construct an object ~
1253
1246
1254
1247
Many languages use the "new" operator to create an object, which is actually
@@ -1312,7 +1305,6 @@ An alternative would have been using the "protected" keyword, just like
1312
1305
"public" changes the access in the other direction. Well, that's just to
1313
1306
reduce the number of keywords.
1314
1307
1315
-
1316
1308
No private object variables ~
1317
1309
1318
1310
Some languages provide several ways to control access to object variables.
0 commit comments