annotate runtime/doc/vim9class.txt @ 34676:5b25ec43f208 v9.1.0219

patch 9.1.0219: Vim9: No enum support Commit: https://github.com/vim/vim/commit/3164cf8f12f14b725b918e3170bb0a9085af8298 Author: Yegappan Lakshmanan <yegappan@yahoo.com> Date: Thu Mar 28 10:36:42 2024 +0100 patch 9.1.0219: Vim9: No enum support Problem: No enum support Solution: Implement enums for Vim9 script (Yegappan Lakshmanan) closes: #14224 Signed-off-by: Yegappan Lakshmanan <yegappan@yahoo.com> Signed-off-by: Christian Brabandt <cb@256bit.org>
author Christian Brabandt <cb@256bit.org>
date Thu, 28 Mar 2024 10:45:06 +0100
parents 5c1a025192ed
children 5914f369ff5b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
34676
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
1 *vim9class.txt* For Vim version 9.1. Last change: 2024 Mar 28
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
34011
62d771d915fe runtime(doc): list of new/changed features in version9.txt
Christian Brabandt <cb@256bit.org>
parents: 34008
diff changeset
7 Vim9 classes, objects, interfaces, types and enums. *vim9-class*
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 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
10 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
11 3. Class variables and methods |Vim9-class-member|
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
12 4. Using an abstract class |Vim9-abstract-class|
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
13 5. Using an interface |Vim9-using-interface|
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
14 6. More class details |Vim9-class|
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
15 7. Type definition |Vim9-type|
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
16 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
17
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
18 9. Rationale
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
19 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
20
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
21 ==============================================================================
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
22
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
23 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
24
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
25 The fancy term is "object-oriented programming". You can find lots of study
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
26 material on this subject. Here we document what |Vim9| script provides,
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
27 assuming you know the basics already. Added are helpful hints about how to
33636
53416c49a7ab patch 9.0.2059: outstanding exceptions may be skipped
Christian Brabandt <cb@256bit.org>
parents: 33490
diff changeset
28 use this functionality effectively. Vim9 classes and objects cannot be used
53416c49a7ab patch 9.0.2059: outstanding exceptions may be skipped
Christian Brabandt <cb@256bit.org>
parents: 33490
diff changeset
29 in legacy Vim scripts and legacy functions.
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
30
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
31 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
32 - 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
33 have a value.
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
34 - 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
35 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
36 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
37 - 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
38 methods.
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
39 - Objects are created from a class and all objects have the same interface.
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
40 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
41
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
42 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
43 - 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
44 This method is invoked on the class name: MyClass.new().
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
45 - 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
46 - 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
47
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
48 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
49 - 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
50 - 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
51
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
52 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
53 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
54
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
55
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
56 Class modeling ~
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 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
59 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
60 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
61 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
62 should match your purpose.
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
63
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
64 Keep in mind that composition (an object contains other objects) is often
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
65 better than inheritance (an object extends another object). Don't waste time
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
66 trying to find the optimal class model. Or waste time discussing whether a
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
67 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
68
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
69
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 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
73
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
74 Let's start with a simple example: a class that stores a text position (see
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
75 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
76
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
77 class TextPosition
33942
3bba09502b8d patch 9.0.2167: Vim9: not consistently using :var for declarations
Christian Brabandt <cb@256bit.org>
parents: 33931
diff changeset
78 var lnum: number
3bba09502b8d patch 9.0.2167: Vim9: not consistently using :var for declarations
Christian Brabandt <cb@256bit.org>
parents: 33931
diff changeset
79 var col: number
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
80
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
81 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
82 this.lnum = lnum
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
83 this.col = col
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
84 enddef
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
85
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
86 def SetLnum(lnum: number)
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
87 this.lnum = lnum
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
88 enddef
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
89
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
90 def SetCol(col: number)
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
91 this.col = col
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
92 enddef
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
93
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
94 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
95 this.lnum = lnum
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
96 this.col = col
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
97 enddef
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
98 endclass
31430
e5e95e8c78a7 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
99 < *object* *Object*
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
100 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
101
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
102 var pos = TextPosition.new(1, 1)
34008
9fc35ba5feb7 runtime(doc): add some error codes to `:help vim9class` (#13747)
Christian Brabandt <cb@256bit.org>
parents: 33996
diff changeset
103 <
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
104 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
105
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
106 echo $'The text position is ({pos.lnum}, {pos.col})'
34056
c1074a41895e runtime(doc): add missing help tags
Christian Brabandt <cb@256bit.org>
parents: 34033
diff changeset
107 < *E1317* *E1327* *:this*
34008
9fc35ba5feb7 runtime(doc): add some error codes to `:help vim9class` (#13747)
Christian Brabandt <cb@256bit.org>
parents: 33996
diff changeset
108 If you have been using other object-oriented languages you will notice that in
34098
8241df3e6dc1 runtime(doc): Improve the documentation for Vim9 classes (#13828)
Christian Brabandt <cb@256bit.org>
parents: 34057
diff changeset
109 Vim, within a class definition, the declared object members are consistently
8241df3e6dc1 runtime(doc): Improve the documentation for Vim9 classes (#13828)
Christian Brabandt <cb@256bit.org>
parents: 34057
diff changeset
110 referred to with the "this." prefix. This is different from languages like
8241df3e6dc1 runtime(doc): Improve the documentation for Vim9 classes (#13828)
Christian Brabandt <cb@256bit.org>
parents: 34057
diff changeset
111 Java and TypeScript. The naming convention makes the object members easy to
8241df3e6dc1 runtime(doc): Improve the documentation for Vim9 classes (#13828)
Christian Brabandt <cb@256bit.org>
parents: 34057
diff changeset
112 spot. Also, when a variable does not have the "this." prefix you know it is
8241df3e6dc1 runtime(doc): Improve the documentation for Vim9 classes (#13828)
Christian Brabandt <cb@256bit.org>
parents: 34057
diff changeset
113 not an object variable.
34008
9fc35ba5feb7 runtime(doc): add some error codes to `:help vim9class` (#13747)
Christian Brabandt <cb@256bit.org>
parents: 33996
diff changeset
114 *E1411*
9fc35ba5feb7 runtime(doc): add some error codes to `:help vim9class` (#13747)
Christian Brabandt <cb@256bit.org>
parents: 33996
diff changeset
115 From outside the class definition, access an object's methods and variables by
9fc35ba5feb7 runtime(doc): add some error codes to `:help vim9class` (#13747)
Christian Brabandt <cb@256bit.org>
parents: 33996
diff changeset
116 using the object name followed by a dot following by the member: >
9fc35ba5feb7 runtime(doc): add some error codes to `:help vim9class` (#13747)
Christian Brabandt <cb@256bit.org>
parents: 33996
diff changeset
117
9fc35ba5feb7 runtime(doc): add some error codes to `:help vim9class` (#13747)
Christian Brabandt <cb@256bit.org>
parents: 33996
diff changeset
118 pos.lnum
34098
8241df3e6dc1 runtime(doc): Improve the documentation for Vim9 classes (#13828)
Christian Brabandt <cb@256bit.org>
parents: 34057
diff changeset
119 pos.SetCol(10)
34008
9fc35ba5feb7 runtime(doc): add some error codes to `:help vim9class` (#13747)
Christian Brabandt <cb@256bit.org>
parents: 33996
diff changeset
120 <
9fc35ba5feb7 runtime(doc): add some error codes to `:help vim9class` (#13747)
Christian Brabandt <cb@256bit.org>
parents: 33996
diff changeset
121 *E1405* *E1406*
9fc35ba5feb7 runtime(doc): add some error codes to `:help vim9class` (#13747)
Christian Brabandt <cb@256bit.org>
parents: 33996
diff changeset
122 A class name cannot be used as an expression. A class name cannot be used in
9fc35ba5feb7 runtime(doc): add some error codes to `:help vim9class` (#13747)
Christian Brabandt <cb@256bit.org>
parents: 33996
diff changeset
123 the left-hand-side of an assignment.
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
124
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
125
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
126 Object variable write access ~
33738
2172872dfbcd patch 9.0.2096: Vim9: confusing usage of private
Christian Brabandt <cb@256bit.org>
parents: 33698
diff changeset
127 *read-only-variable*
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
128 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
129
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
130 pos.lnum = 9
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
131 < *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
132 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
133 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
134 it: >
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
135
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
136 pos.SetLnum(9)
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
137
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
138 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
139 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
140 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
141 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
142 the closest valid value.
33738
2172872dfbcd patch 9.0.2096: Vim9: confusing usage of private
Christian Brabandt <cb@256bit.org>
parents: 33698
diff changeset
143 *:public* *public-variable* *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
144 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
145 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
146
34033
cf39f47f26df runtime(doc): correct the :public example (#13795)
Christian Brabandt <cb@256bit.org>
parents: 34025
diff changeset
147 public var lnum: number
cf39f47f26df runtime(doc): correct the :public example (#13795)
Christian Brabandt <cb@256bit.org>
parents: 34025
diff changeset
148 public var col: number
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
149
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
150 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
151 "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
152 *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
153 If you try to set an object variable that doesn't exist you get an error: >
31579
7d68a90cbf5c Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
154 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
155 < 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
156
33286
0c3553cfe22e patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents: 33217
diff changeset
157 *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
158 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
159
33738
2172872dfbcd patch 9.0.2096: Vim9: confusing usage of private
Christian Brabandt <cb@256bit.org>
parents: 33698
diff changeset
160 Protected variables ~
2172872dfbcd patch 9.0.2096: Vim9: confusing usage of private
Christian Brabandt <cb@256bit.org>
parents: 33698
diff changeset
161 *protected-variable* *E1332* *E1333*
2172872dfbcd patch 9.0.2096: Vim9: confusing usage of private
Christian Brabandt <cb@256bit.org>
parents: 33698
diff changeset
162 On the other hand, if you do not want the object variables to be read directly
2172872dfbcd patch 9.0.2096: Vim9: confusing usage of private
Christian Brabandt <cb@256bit.org>
parents: 33698
diff changeset
163 from outside the class or its sub-classes, you can make them protected. This
2172872dfbcd patch 9.0.2096: Vim9: confusing usage of private
Christian Brabandt <cb@256bit.org>
parents: 33698
diff changeset
164 is done by prefixing an underscore to the name: >
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
165
33942
3bba09502b8d patch 9.0.2167: Vim9: not consistently using :var for declarations
Christian Brabandt <cb@256bit.org>
parents: 33931
diff changeset
166 var _lnum: number
34098
8241df3e6dc1 runtime(doc): Improve the documentation for Vim9 classes (#13828)
Christian Brabandt <cb@256bit.org>
parents: 34057
diff changeset
167 var _col: number
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
168
33738
2172872dfbcd patch 9.0.2096: Vim9: confusing usage of private
Christian Brabandt <cb@256bit.org>
parents: 33698
diff changeset
169 Now you need to provide methods to get the value of the protected variables.
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
170 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
171 "Get": >
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
172
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
173 def GetLnum(): number
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
174 return this._lnum
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
175 enddef
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
176
34098
8241df3e6dc1 runtime(doc): Improve the documentation for Vim9 classes (#13828)
Christian Brabandt <cb@256bit.org>
parents: 34057
diff changeset
177 def GetCol(): number
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
178 return this._col
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
179 enddef
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
180
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
181 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
182 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
183 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
184
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
185 def GetLnum(): number
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
186 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
187 return this._lineCount
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
188 endif
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
189 return this._lnum
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
190 enddef
33286
0c3553cfe22e patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents: 33217
diff changeset
191 <
33738
2172872dfbcd patch 9.0.2096: Vim9: confusing usage of private
Christian Brabandt <cb@256bit.org>
parents: 33698
diff changeset
192 Protected methods ~
2172872dfbcd patch 9.0.2096: Vim9: confusing usage of private
Christian Brabandt <cb@256bit.org>
parents: 33698
diff changeset
193 *protected-method* *E1366*
33025
1d18c7fe609f patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents: 32960
diff changeset
194 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
195 same class and not used from outside the class, then you can make them
33738
2172872dfbcd patch 9.0.2096: Vim9: confusing usage of private
Christian Brabandt <cb@256bit.org>
parents: 33698
diff changeset
196 protected. This is done by prefixing the method name with an underscore: >
33025
1d18c7fe609f patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents: 32960
diff changeset
197
1d18c7fe609f patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents: 32960
diff changeset
198 class SomeClass
1d18c7fe609f patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents: 32960
diff changeset
199 def _Foo(): number
1d18c7fe609f patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents: 32960
diff changeset
200 return 10
1d18c7fe609f patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents: 32960
diff changeset
201 enddef
1d18c7fe609f patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents: 32960
diff changeset
202 def Bar(): number
1d18c7fe609f patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents: 32960
diff changeset
203 return this._Foo()
1d18c7fe609f patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents: 32960
diff changeset
204 enddef
1d18c7fe609f patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents: 32960
diff changeset
205 endclass
1d18c7fe609f patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents: 32960
diff changeset
206 <
33738
2172872dfbcd patch 9.0.2096: Vim9: confusing usage of private
Christian Brabandt <cb@256bit.org>
parents: 33698
diff changeset
207 Accessing a protected method outside the class will result in an error (using
33025
1d18c7fe609f patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents: 32960
diff changeset
208 the above class): >
1d18c7fe609f patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents: 32960
diff changeset
209
1d18c7fe609f patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents: 32960
diff changeset
210 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
211 a._Foo()
1d18c7fe609f patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents: 32960
diff changeset
212 <
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
213 Simplifying the new() method ~
33383
5c12ca4dcd45 patch 9.0.1950: Vim9: error codes spread out
Christian Brabandt <cb@256bit.org>
parents: 33379
diff changeset
214 *new()* *constructor*
34025
385c61388fc3 runtime(doc): add help tag multiple-constructors. (#13772)
Christian Brabandt <cb@256bit.org>
parents: 34012
diff changeset
215 See also |default-constructor| and |multiple-constructors|.
385c61388fc3 runtime(doc): add help tag multiple-constructors. (#13772)
Christian Brabandt <cb@256bit.org>
parents: 34012
diff changeset
216
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
217 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
218 see this pattern: >
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
219
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
220 class SomeClass
33942
3bba09502b8d patch 9.0.2167: Vim9: not consistently using :var for declarations
Christian Brabandt <cb@256bit.org>
parents: 33931
diff changeset
221 var lnum: number
3bba09502b8d patch 9.0.2167: Vim9: not consistently using :var for declarations
Christian Brabandt <cb@256bit.org>
parents: 33931
diff changeset
222 var col: number
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
223
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
224 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
225 this.lnum = lnum
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
226 this.col = col
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
227 enddef
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
228 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
229 <
7c9124711f99 patch 9.0.1948: Vim9: object variable "this." should only be used in constructor
Christian Brabandt <cb@256bit.org>
parents: 33372
diff changeset
230 *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
231 Not only is this text you need to write, it also has the type of each
33945
27746ed6cb05 runtime(doc): grammar & typo fixes
Christian Brabandt <cb@256bit.org>
parents: 33942
diff changeset
232 variable twice. Since this is so common a shorter way to write new() is
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 provided: >
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
234
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
235 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
236 enddef
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
237
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
238 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
239 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
240 new() call is assigned to that object variable. This mechanism comes from the
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
241 Dart language.
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
242
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
243 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
244 results in a much shorter class definition than what we started with: >
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
245
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
246 class TextPosition
33942
3bba09502b8d patch 9.0.2167: Vim9: not consistently using :var for declarations
Christian Brabandt <cb@256bit.org>
parents: 33931
diff changeset
247 public var lnum: number
3bba09502b8d patch 9.0.2167: Vim9: not consistently using :var for declarations
Christian Brabandt <cb@256bit.org>
parents: 33931
diff changeset
248 public var col: number
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
249
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
250 def new(this.lnum, this.col)
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
251 enddef
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
252
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
253 def SetPosition(lnum: number, col: number)
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
254 this.lnum = lnum
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
255 this.col = col
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
256 enddef
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
257 endclass
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
258
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
259 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
260 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
261 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
262 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
263 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
264 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
265 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
266
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
267 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
268 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
269 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
270
33385
b5ad84fdc702 patch 9.0.1951: Vim9: hard to debug vim9_class errors from CI
Christian Brabandt <cb@256bit.org>
parents: 33383
diff changeset
271 *E1365*
32903
54c01bb98b8e patch 9.0.1760: vim9 class problem with new() constructor
Christian Brabandt <cb@256bit.org>
parents: 32294
diff changeset
272 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
273 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
274
33385
b5ad84fdc702 patch 9.0.1951: Vim9: hard to debug vim9_class errors from CI
Christian Brabandt <cb@256bit.org>
parents: 33383
diff changeset
275 *E1386*
b5ad84fdc702 patch 9.0.1951: Vim9: hard to debug vim9_class errors from CI
Christian Brabandt <cb@256bit.org>
parents: 33383
diff changeset
276 When invoking an object method, the method name should be preceded by the
33945
27746ed6cb05 runtime(doc): grammar & typo fixes
Christian Brabandt <cb@256bit.org>
parents: 33942
diff changeset
277 object variable name. An object method cannot be invoked using the class
33385
b5ad84fdc702 patch 9.0.1951: Vim9: hard to debug vim9_class errors from CI
Christian Brabandt <cb@256bit.org>
parents: 33383
diff changeset
278 name.
b5ad84fdc702 patch 9.0.1951: Vim9: hard to debug vim9_class errors from CI
Christian Brabandt <cb@256bit.org>
parents: 33383
diff changeset
279
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
280 ==============================================================================
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
281
33286
0c3553cfe22e patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents: 33217
diff changeset
282 3. Class Variables and Methods *Vim9-class-member*
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
283
33286
0c3553cfe22e patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents: 33217
diff changeset
284 *:static* *E1337* *E1338* *E1368*
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
285 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
286 prefix in the class where they are defined: >
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
287
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
288 class OtherThing
33942
3bba09502b8d patch 9.0.2167: Vim9: not consistently using :var for declarations
Christian Brabandt <cb@256bit.org>
parents: 33931
diff changeset
289 var size: number
3bba09502b8d patch 9.0.2167: Vim9: not consistently using :var for declarations
Christian Brabandt <cb@256bit.org>
parents: 33931
diff changeset
290 static var totalSize: number
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
291
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
292 def new(this.size)
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
293 totalSize += this.size
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
294 enddef
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
295 endclass
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
296 < *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
297 Since the name is used as-is, shadowing the name by a method argument name
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
298 or local variable name is not allowed.
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
299
33385
b5ad84fdc702 patch 9.0.1951: Vim9: hard to debug vim9_class errors from CI
Christian Brabandt <cb@256bit.org>
parents: 33383
diff changeset
300 *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
301 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
302 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
303
33738
2172872dfbcd patch 9.0.2096: Vim9: confusing usage of private
Christian Brabandt <cb@256bit.org>
parents: 33698
diff changeset
304 Just like object members the access can be made protected by using an
2172872dfbcd patch 9.0.2096: Vim9: confusing usage of private
Christian Brabandt <cb@256bit.org>
parents: 33698
diff changeset
305 underscore as the first character in the name, and it can be made public by
2172872dfbcd patch 9.0.2096: Vim9: confusing usage of private
Christian Brabandt <cb@256bit.org>
parents: 33698
diff changeset
306 prefixing "public": >
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
307
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
308 class OtherThing
33942
3bba09502b8d patch 9.0.2167: Vim9: not consistently using :var for declarations
Christian Brabandt <cb@256bit.org>
parents: 33931
diff changeset
309 static var total: number # anybody can read, only class can write
3bba09502b8d patch 9.0.2167: Vim9: not consistently using :var for declarations
Christian Brabandt <cb@256bit.org>
parents: 33931
diff changeset
310 static var _sum: number # only class can read and write
3bba09502b8d patch 9.0.2167: Vim9: not consistently using :var for declarations
Christian Brabandt <cb@256bit.org>
parents: 33931
diff changeset
311 public static var result: number # anybody can read and write
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
312 endclass
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
313 <
33286
0c3553cfe22e patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents: 33217
diff changeset
314 *class-method*
0c3553cfe22e patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents: 33217
diff changeset
315 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
316 variables but they have no access to the object variables, they cannot use the
33434
484543479bd7 runtime(doc): fix typos.
Christian Brabandt <cb@256bit.org>
parents: 33432
diff changeset
317 "this" keyword:
33286
0c3553cfe22e patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents: 33217
diff changeset
318 >
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
319 class OtherThing
33942
3bba09502b8d patch 9.0.2167: Vim9: not consistently using :var for declarations
Christian Brabandt <cb@256bit.org>
parents: 33931
diff changeset
320 var size: number
3bba09502b8d patch 9.0.2167: Vim9: not consistently using :var for declarations
Christian Brabandt <cb@256bit.org>
parents: 33931
diff changeset
321 static var totalSize: number
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
322
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
323 # Clear the total size and return the value it had before.
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
324 static def ClearTotalSize(): number
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
325 var prev = totalSize
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
326 totalSize = 0
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
327 return prev
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
328 enddef
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
329 endclass
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
330
33286
0c3553cfe22e patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents: 33217
diff changeset
331 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
332 class the class name must be prefixed: `OtherThing.ClearTotalSize()`. To use
34098
8241df3e6dc1 runtime(doc): Improve the documentation for Vim9 classes (#13828)
Christian Brabandt <cb@256bit.org>
parents: 34057
diff changeset
333 a class method from a parent class in a child class, the class name must be
8241df3e6dc1 runtime(doc): Improve the documentation for Vim9 classes (#13828)
Christian Brabandt <cb@256bit.org>
parents: 34057
diff changeset
334 prefixed.
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
335
33738
2172872dfbcd patch 9.0.2096: Vim9: confusing usage of private
Christian Brabandt <cb@256bit.org>
parents: 33698
diff changeset
336 Just like object methods the access can be made protected by using an
2172872dfbcd patch 9.0.2096: Vim9: confusing usage of private
Christian Brabandt <cb@256bit.org>
parents: 33698
diff changeset
337 underscore as the first character in the method name: >
33025
1d18c7fe609f patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents: 32960
diff changeset
338
1d18c7fe609f patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents: 32960
diff changeset
339 class OtherThing
1d18c7fe609f patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents: 32960
diff changeset
340 static def _Foo()
1d18c7fe609f patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents: 32960
diff changeset
341 echo "Foo"
1d18c7fe609f patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents: 32960
diff changeset
342 enddef
1d18c7fe609f patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents: 32960
diff changeset
343 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
344 _Foo()
33025
1d18c7fe609f patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents: 32960
diff changeset
345 enddef
1d18c7fe609f patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents: 32960
diff changeset
346 endclass
33068
d42927c6e556 patch 9.0.1821: Vim9 constructors are always static
Christian Brabandt <cb@256bit.org>
parents: 33025
diff changeset
347 <
d42927c6e556 patch 9.0.1821: Vim9 constructors are always static
Christian Brabandt <cb@256bit.org>
parents: 33025
diff changeset
348 *E1370*
34012
291b4ea552eb runtime(doc): Clarify that new() is not static
Christian Brabandt <cb@256bit.org>
parents: 34011
diff changeset
349 Note that constructors cannot be declared as "static". They are called like a
291b4ea552eb runtime(doc): Clarify that new() is not static
Christian Brabandt <cb@256bit.org>
parents: 34011
diff changeset
350 static but execute as an object method; they have access to "this".
33025
1d18c7fe609f patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents: 32960
diff changeset
351
33286
0c3553cfe22e patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents: 33217
diff changeset
352 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
353 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
354 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
355
0c3553cfe22e patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents: 33217
diff changeset
356 vim9script
0c3553cfe22e patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents: 33217
diff changeset
357 class Vehicle
33942
3bba09502b8d patch 9.0.2167: Vim9: not consistently using :var for declarations
Christian Brabandt <cb@256bit.org>
parents: 33931
diff changeset
358 static var nextID: number = 1000
33286
0c3553cfe22e patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents: 33217
diff changeset
359 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
360 nextID += 1
0c3553cfe22e patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents: 33217
diff changeset
361 return nextID
0c3553cfe22e patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents: 33217
diff changeset
362 enddef
0c3553cfe22e patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents: 33217
diff changeset
363 endclass
0c3553cfe22e patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents: 33217
diff changeset
364 class Car extends Vehicle
33942
3bba09502b8d patch 9.0.2167: Vim9: not consistently using :var for declarations
Christian Brabandt <cb@256bit.org>
parents: 33931
diff changeset
365 var myID: number
33286
0c3553cfe22e patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents: 33217
diff changeset
366 def new()
0c3553cfe22e patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents: 33217
diff changeset
367 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
368 enddef
0c3553cfe22e patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents: 33217
diff changeset
369 endclass
0c3553cfe22e patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents: 33217
diff changeset
370 <
0c3553cfe22e patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents: 33217
diff changeset
371 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
372 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
373 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
374 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
375 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
376
33996
6d33f47b71b8 patch 9.0.2182: Vim9: need a way to reserve future extension
Christian Brabandt <cb@256bit.org>
parents: 33951
diff changeset
377 The double underscore (__) prefix for a class or object method name is
6d33f47b71b8 patch 9.0.2182: Vim9: need a way to reserve future extension
Christian Brabandt <cb@256bit.org>
parents: 33951
diff changeset
378 reserved for future use.
6d33f47b71b8 patch 9.0.2182: Vim9: need a way to reserve future extension
Christian Brabandt <cb@256bit.org>
parents: 33951
diff changeset
379
33951
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
380 *object-final-variable* *E1409*
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
381 The |:final| keyword can be used to make a class or object variable a
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
382 constant. Examples: >
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
383
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
384 class A
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
385 final v1 = [1, 2] # final object variable
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
386 public final v2 = {x: 1} # final object variable
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
387 static final v3 = 'abc' # final class variable
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
388 public static final v4 = 0z10 # final class variable
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
389 endclass
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
390 <
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
391 A final variable can be changed only from a constructor function. Example: >
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
392
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
393 class A
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
394 final v1: list<number>
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
395 def new()
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
396 this.v1 = [1, 2]
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
397 enddef
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
398 endclass
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
399 var a = A.new()
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
400 echo a.v1
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
401 <
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
402 Note that the value of a final variable can be changed. Example: >
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
403
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
404 class A
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
405 public final v1 = [1, 2]
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
406 endclass
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
407 var a = A.new()
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
408 a.v1[0] = 6 # OK
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
409 a.v1->add(3) # OK
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
410 a.v1 = [3, 4] # Error
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
411 <
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
412 *E1408*
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
413 Final variables are not supported in an interface. A class or object method
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
414 cannot be final.
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
415
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
416 *object-const-variable*
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
417 The |:const| keyword can be used to make a class or object variable and the
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
418 value a constant. Examples: >
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
419
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
420 class A
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
421 const v1 = [1, 2] # const object variable
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
422 public const v2 = {x: 1} # const object variable
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
423 static const v3 = 'abc' # const class variable
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
424 public static const v4 = 0z10 # const class variable
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
425 endclass
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
426 <
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
427 A const variable can be changed only from a constructor function. Example: >
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
428
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
429 class A
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
430 const v1: list<number>
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
431 def new()
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
432 this.v1 = [1, 2]
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
433 enddef
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
434 endclass
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
435 var a = A.new()
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
436 echo a.v1
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
437 <
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
438 A const variable and its value cannot be changed. Example: >
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
439
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
440 class A
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
441 public const v1 = [1, 2]
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
442 endclass
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
443 var a = A.new()
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
444 a.v1[0] = 6 # Error
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
445 a.v1->add(3) # Error
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
446 a.v1 = [3, 4] # Error
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
447 <
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
448 *E1410*
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
449 Const variables are not supported in an interface. A class or object method
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
450 cannot be a const.
45a50fd59a73 patch 9.0.2170: Vim9: no support for const/final class/objects vars
Christian Brabandt <cb@256bit.org>
parents: 33945
diff changeset
451
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
452 ==============================================================================
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
453
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
454 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
455
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
456 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
457 model one often finds that a few classes have the same properties that can be
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
458 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
459 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
460 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
461
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
462 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
463 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
464 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
465 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
466
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
467 abstract class Shape
33942
3bba09502b8d patch 9.0.2167: Vim9: not consistently using :var for declarations
Christian Brabandt <cb@256bit.org>
parents: 33931
diff changeset
468 var color = Color.Black
3bba09502b8d patch 9.0.2167: Vim9: not consistently using :var for declarations
Christian Brabandt <cb@256bit.org>
parents: 33931
diff changeset
469 var thickness = 10
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
470 endclass
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
471
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
472 class Square extends Shape
33942
3bba09502b8d patch 9.0.2167: Vim9: not consistently using :var for declarations
Christian Brabandt <cb@256bit.org>
parents: 33931
diff changeset
473 var size: number
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
474
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
475 def new(this.size)
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
476 enddef
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
477 endclass
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
478
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
479 class Triangle extends Shape
33942
3bba09502b8d patch 9.0.2167: Vim9: not consistently using :var for declarations
Christian Brabandt <cb@256bit.org>
parents: 33931
diff changeset
480 var base: number
3bba09502b8d patch 9.0.2167: Vim9: not consistently using :var for declarations
Christian Brabandt <cb@256bit.org>
parents: 33931
diff changeset
481 var height: number
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
482
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
483 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
484 enddef
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
485 endclass
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
486 <
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
487 An abstract class is defined the same way as a normal class, except that it
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
488 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
489
33798
af1b18018eeb runtime(doc): Fix whitespace and formatting of some help files (#13549)
Christian Brabandt <cb@256bit.org>
parents: 33738
diff changeset
490 *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
491 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
492 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
493
499ba27ba0f6 patch 9.0.1885: Vim9: no support for abstract methods
Christian Brabandt <cb@256bit.org>
parents: 33068
diff changeset
494 abstract class Shape
499ba27ba0f6 patch 9.0.1885: Vim9: no support for abstract methods
Christian Brabandt <cb@256bit.org>
parents: 33068
diff changeset
495 abstract def Draw()
499ba27ba0f6 patch 9.0.1885: Vim9: no support for abstract methods
Christian Brabandt <cb@256bit.org>
parents: 33068
diff changeset
496 endclass
33286
0c3553cfe22e patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents: 33217
diff changeset
497 <
33698
643db54ed3e7 patch 9.0.2084: Vim9: abstract static methods are possible
Christian Brabandt <cb@256bit.org>
parents: 33682
diff changeset
498 A static method in an abstract class cannot be an abstract method.
643db54ed3e7 patch 9.0.2084: Vim9: abstract static methods are possible
Christian Brabandt <cb@256bit.org>
parents: 33682
diff changeset
499
33286
0c3553cfe22e patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents: 33217
diff changeset
500 *E1373*
34098
8241df3e6dc1 runtime(doc): Improve the documentation for Vim9 classes (#13828)
Christian Brabandt <cb@256bit.org>
parents: 34057
diff changeset
501 A non-abstract class extending the abstract class must implement all the
8241df3e6dc1 runtime(doc): Improve the documentation for Vim9 classes (#13828)
Christian Brabandt <cb@256bit.org>
parents: 34057
diff changeset
502 abstract methods. The signature (arguments, argument types and return type)
8241df3e6dc1 runtime(doc): Improve the documentation for Vim9 classes (#13828)
Christian Brabandt <cb@256bit.org>
parents: 34057
diff changeset
503 must be exactly the same. If the return type of a method is a class, then
8241df3e6dc1 runtime(doc): Improve the documentation for Vim9 classes (#13828)
Christian Brabandt <cb@256bit.org>
parents: 34057
diff changeset
504 that class or one of its subclasses can be used in the extended method.
31335
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 ==============================================================================
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
507
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
508 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
509
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
510 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
511 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
512 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
513 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
514
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
515 abstract class Shape
33942
3bba09502b8d patch 9.0.2167: Vim9: not consistently using :var for declarations
Christian Brabandt <cb@256bit.org>
parents: 33931
diff changeset
516 var color = Color.Black
3bba09502b8d patch 9.0.2167: Vim9: not consistently using :var for declarations
Christian Brabandt <cb@256bit.org>
parents: 33931
diff changeset
517 var thickness = 10
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
518 endclass
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
519
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
520 interface HasSurface
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
521 def Surface(): number
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
522 endinterface
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
523
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
524 class Square extends Shape implements HasSurface
33942
3bba09502b8d patch 9.0.2167: Vim9: not consistently using :var for declarations
Christian Brabandt <cb@256bit.org>
parents: 33931
diff changeset
525 var size: number
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
526
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
527 def new(this.size)
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
528 enddef
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
529
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
530 def Surface(): number
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
531 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
532 enddef
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
533 endclass
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
534
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
535 class Triangle extends Shape implements HasSurface
33942
3bba09502b8d patch 9.0.2167: Vim9: not consistently using :var for declarations
Christian Brabandt <cb@256bit.org>
parents: 33931
diff changeset
536 var base: number
3bba09502b8d patch 9.0.2167: Vim9: not consistently using :var for declarations
Christian Brabandt <cb@256bit.org>
parents: 33931
diff changeset
537 var height: number
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
538
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
539 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
540 enddef
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
541
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
542 def Surface(): number
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
543 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
544 enddef
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
545 endclass
33286
0c3553cfe22e patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents: 33217
diff changeset
546 <
0c3553cfe22e patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents: 33217
diff changeset
547 *E1348* *E1349* *E1367* *E1382* *E1383*
31671
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
548 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
549 interface must appear in the class, with the same types.
31671
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
550
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
551 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
552
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
553 var shapes: list<HasSurface> = [
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
554 Square.new(12),
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
555 Triangle.new(8, 15),
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
556 ]
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
557 for shape in shapes
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
558 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
559 endfor
33286
0c3553cfe22e patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents: 33217
diff changeset
560 <
33372
3e9a91624b40 patch 9.0.1945: Vim9: missing support for ro-vars in interface
Christian Brabandt <cb@256bit.org>
parents: 33351
diff changeset
561 *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
562 An interface can contain only object methods and read-only object variables.
33738
2172872dfbcd patch 9.0.2096: Vim9: confusing usage of private
Christian Brabandt <cb@256bit.org>
parents: 33698
diff changeset
563 An interface cannot contain read-write or protected object variables,
2172872dfbcd patch 9.0.2096: Vim9: confusing usage of private
Christian Brabandt <cb@256bit.org>
parents: 33698
diff changeset
564 protected 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
565
33286
0c3553cfe22e patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents: 33217
diff changeset
566 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
567 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
568
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
569 ==============================================================================
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
570
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
571 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
572
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
573 Defining a class ~
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
574 *:class* *:endclass* *:abstract*
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
575 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
576 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
577
31430
e5e95e8c78a7 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
578 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
579 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
580
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
581 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
582 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
583 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
584
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
585 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
586 these variants: >
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
587
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
588 class ClassName
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
589 endclass
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
590
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
591 export class ClassName
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
592 endclass
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
593
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
594 abstract class ClassName
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
595 endclass
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
596
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
597 export abstract class ClassName
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
598 endclass
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
599 <
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
600 *E1314*
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
601 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
602 That avoids clashing with builtin types.
31430
e5e95e8c78a7 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
603 *E1315*
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
604 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
605 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
606 extends ClassName
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
607 implements InterfaceName, OtherInterface
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
608 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
609 < *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
610 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
611 define a method with the same name and different type of arguments. It is not
34098
8241df3e6dc1 runtime(doc): Improve the documentation for Vim9 classes (#13828)
Christian Brabandt <cb@256bit.org>
parents: 34057
diff changeset
612 possible to use a public and protected member variable with the same name. An
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
613 object variable name used in a super class cannot be reused in a child class.
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
614
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
615
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
616 Object Variable Initialization ~
33738
2172872dfbcd patch 9.0.2096: Vim9: confusing usage of private
Christian Brabandt <cb@256bit.org>
parents: 33698
diff changeset
617
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
618 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
619 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
620 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
621
33401
bb99820510ef patch 9.0.1959: Vim9: methods parameters and types are covariant
Christian Brabandt <cb@256bit.org>
parents: 33385
diff changeset
622 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
623 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
624 "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
625 "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
626
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
627 Extending a class ~
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
628 *extends*
31671
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
629 A class can extend one other class. *E1352* *E1353* *E1354*
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
630 The basic idea is to build on top of an existing class, add properties to it.
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
631
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
632 The extended class is called the "base class" or "super class". The new class
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
633 is called the "child class".
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
634
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
635 Object variables from the base class are all taken over by the child class. It
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
636 is not possible to override them (unlike some other languages).
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
637
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
638 *E1356* *E1357* *E1358*
33432
97ceabebaeaf patch 9.0.1974: vim9: using contra-variant type-checks
Christian Brabandt <cb@256bit.org>
parents: 33401
diff changeset
639 Object methods of the base class can be overruled. The signature (arguments,
33490
33593174288b runtime(doc): Update vim9class help (#13292)
Christian Brabandt <cb@256bit.org>
parents: 33434
diff changeset
640 argument types and return type) must be exactly the same. If the return type
33593174288b runtime(doc): Update vim9class help (#13292)
Christian Brabandt <cb@256bit.org>
parents: 33434
diff changeset
641 of a method is a class, then that class or one of its subclasses can be used
33593174288b runtime(doc): Update vim9class help (#13292)
Christian Brabandt <cb@256bit.org>
parents: 33434
diff changeset
642 in the extended method. The method of the base class can be called by
33593174288b runtime(doc): Update vim9class help (#13292)
Christian Brabandt <cb@256bit.org>
parents: 33434
diff changeset
643 prefixing "super.".
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
644
33286
0c3553cfe22e patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents: 33217
diff changeset
645 *E1377*
33738
2172872dfbcd patch 9.0.2096: Vim9: confusing usage of private
Christian Brabandt <cb@256bit.org>
parents: 33698
diff changeset
646 The access level of a method (public or protected) in a child class should be
33286
0c3553cfe22e patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents: 33217
diff changeset
647 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
648
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
649 Other object methods of the base class are taken over by the child class.
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
650
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
651 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
652 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
653 the name of the class (for class methods) or "super.".
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
654
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
655 Unlike other languages, the constructor of the base class does not need to be
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
656 invoked. In fact, it cannot be invoked. If some initialization from the base
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
657 class also needs to be done in a child class, put it in an object method and
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
658 call that method from every constructor().
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
659
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 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
661 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
662 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
663 will be added automatically.
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
664
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
665
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
666 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
667 *implements* *E1346* *E1347* *E1389*
31671
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
668 A class can implement one or more interfaces. The "implements" keyword can
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
669 only appear once *E1350* . Multiple interfaces can be specified, separated by
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
670 commas. Each interface name can appear only once. *E1351*
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
671
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
672
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
673 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
674 *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
675 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
676 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
677 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
678
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
679
31430
e5e95e8c78a7 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
680 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
681 *E1318* *E1325* *E1388*
32061
b2412874362f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 32004
diff changeset
682 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
683 - An object variable declaration: >
33942
3bba09502b8d patch 9.0.2167: Vim9: not consistently using :var for declarations
Christian Brabandt <cb@256bit.org>
parents: 33931
diff changeset
684 var _protectedVariableName: memberType
3bba09502b8d patch 9.0.2167: Vim9: not consistently using :var for declarations
Christian Brabandt <cb@256bit.org>
parents: 33931
diff changeset
685 var readonlyVariableName: memberType
3bba09502b8d patch 9.0.2167: Vim9: not consistently using :var for declarations
Christian Brabandt <cb@256bit.org>
parents: 33931
diff changeset
686 public var readwriteVariableName: memberType
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
687 - A class variable declaration: >
33942
3bba09502b8d patch 9.0.2167: Vim9: not consistently using :var for declarations
Christian Brabandt <cb@256bit.org>
parents: 33931
diff changeset
688 static var _protectedClassVariableName: memberType
3bba09502b8d patch 9.0.2167: Vim9: not consistently using :var for declarations
Christian Brabandt <cb@256bit.org>
parents: 33931
diff changeset
689 static var readonlyClassVariableName: memberType
34098
8241df3e6dc1 runtime(doc): Improve the documentation for Vim9 classes (#13828)
Christian Brabandt <cb@256bit.org>
parents: 34057
diff changeset
690 public static var readwriteClassVariableName: memberType
31430
e5e95e8c78a7 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
691 - A constructor method: >
32004
a9b5ffbc0428 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31885
diff changeset
692 def new(arguments)
a9b5ffbc0428 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31885
diff changeset
693 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
694 - 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
695 static def SomeMethod(arguments)
33738
2172872dfbcd patch 9.0.2096: Vim9: confusing usage of private
Christian Brabandt <cb@256bit.org>
parents: 33698
diff changeset
696 static def _ProtectedMethod(arguments)
31430
e5e95e8c78a7 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
697 - An object method: >
32004
a9b5ffbc0428 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31885
diff changeset
698 def SomeMethod(arguments)
33738
2172872dfbcd patch 9.0.2096: Vim9: confusing usage of private
Christian Brabandt <cb@256bit.org>
parents: 33698
diff changeset
699 def _ProtectedMethod(arguments)
33286
0c3553cfe22e patch 9.0.1909: Vim9: problem calling class method from other class
Christian Brabandt <cb@256bit.org>
parents: 33217
diff changeset
700
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
701 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
702 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
703 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
704 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
705 For example: >
33942
3bba09502b8d patch 9.0.2167: Vim9: not consistently using :var for declarations
Christian Brabandt <cb@256bit.org>
parents: 33931
diff changeset
706 var nameList = []
31579
7d68a90cbf5c Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
707 This specifies a list, but the item type is unknown. Better use: >
33942
3bba09502b8d patch 9.0.2167: Vim9: not consistently using :var for declarations
Christian Brabandt <cb@256bit.org>
parents: 33931
diff changeset
708 var nameList: list<string>
31579
7d68a90cbf5c Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
709 The initialization isn't needed, the list is empty by default.
7d68a90cbf5c Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
710 *E1330*
7d68a90cbf5c Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
711 Some types cannot be used, such as "void", "null" and "v:none".
31430
e5e95e8c78a7 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
712
34472
5c1a025192ed patch 9.1.0148: Vim9: can't call internal methods with objects
Christian Brabandt <cb@256bit.org>
parents: 34112
diff changeset
713 Builtin Object Methods ~
5c1a025192ed patch 9.1.0148: Vim9: can't call internal methods with objects
Christian Brabandt <cb@256bit.org>
parents: 34112
diff changeset
714 *builtin-object-methods*
5c1a025192ed patch 9.1.0148: Vim9: can't call internal methods with objects
Christian Brabandt <cb@256bit.org>
parents: 34112
diff changeset
715 Some of the builtin functions like |empty()|, |len()| and |string()| can be
5c1a025192ed patch 9.1.0148: Vim9: can't call internal methods with objects
Christian Brabandt <cb@256bit.org>
parents: 34112
diff changeset
716 used with an object. An object can implement a method with the same name as
5c1a025192ed patch 9.1.0148: Vim9: can't call internal methods with objects
Christian Brabandt <cb@256bit.org>
parents: 34112
diff changeset
717 these builtin functions to return an object-specific value.
5c1a025192ed patch 9.1.0148: Vim9: can't call internal methods with objects
Christian Brabandt <cb@256bit.org>
parents: 34112
diff changeset
718
5c1a025192ed patch 9.1.0148: Vim9: can't call internal methods with objects
Christian Brabandt <cb@256bit.org>
parents: 34112
diff changeset
719 *E1412*
5c1a025192ed patch 9.1.0148: Vim9: can't call internal methods with objects
Christian Brabandt <cb@256bit.org>
parents: 34112
diff changeset
720 The following builtin methods are supported:
5c1a025192ed patch 9.1.0148: Vim9: can't call internal methods with objects
Christian Brabandt <cb@256bit.org>
parents: 34112
diff changeset
721 *object-empty()*
5c1a025192ed patch 9.1.0148: Vim9: can't call internal methods with objects
Christian Brabandt <cb@256bit.org>
parents: 34112
diff changeset
722 empty() Invoked by the |empty()| function to check whether an object is
5c1a025192ed patch 9.1.0148: Vim9: can't call internal methods with objects
Christian Brabandt <cb@256bit.org>
parents: 34112
diff changeset
723 empty. If this method is missing, then true is returned. This
5c1a025192ed patch 9.1.0148: Vim9: can't call internal methods with objects
Christian Brabandt <cb@256bit.org>
parents: 34112
diff changeset
724 method should not accept any arguments and must return a boolean.
5c1a025192ed patch 9.1.0148: Vim9: can't call internal methods with objects
Christian Brabandt <cb@256bit.org>
parents: 34112
diff changeset
725 *object-len()*
5c1a025192ed patch 9.1.0148: Vim9: can't call internal methods with objects
Christian Brabandt <cb@256bit.org>
parents: 34112
diff changeset
726 len() Invoked by the |len()| function to return the length of an
5c1a025192ed patch 9.1.0148: Vim9: can't call internal methods with objects
Christian Brabandt <cb@256bit.org>
parents: 34112
diff changeset
727 object. If this method is missing in the class, then an error is
5c1a025192ed patch 9.1.0148: Vim9: can't call internal methods with objects
Christian Brabandt <cb@256bit.org>
parents: 34112
diff changeset
728 given and zero is returned. This method should not accept any
5c1a025192ed patch 9.1.0148: Vim9: can't call internal methods with objects
Christian Brabandt <cb@256bit.org>
parents: 34112
diff changeset
729 arguments and must return a number.
5c1a025192ed patch 9.1.0148: Vim9: can't call internal methods with objects
Christian Brabandt <cb@256bit.org>
parents: 34112
diff changeset
730 *object-string()*
5c1a025192ed patch 9.1.0148: Vim9: can't call internal methods with objects
Christian Brabandt <cb@256bit.org>
parents: 34112
diff changeset
731 string() Invoked by the |string()| function to get a textual
5c1a025192ed patch 9.1.0148: Vim9: can't call internal methods with objects
Christian Brabandt <cb@256bit.org>
parents: 34112
diff changeset
732 representation of an object. Also used by the |:echo| command
5c1a025192ed patch 9.1.0148: Vim9: can't call internal methods with objects
Christian Brabandt <cb@256bit.org>
parents: 34112
diff changeset
733 for an object. If this method is missing in the class, then a
5c1a025192ed patch 9.1.0148: Vim9: can't call internal methods with objects
Christian Brabandt <cb@256bit.org>
parents: 34112
diff changeset
734 built-in default textual representation is used. This method
5c1a025192ed patch 9.1.0148: Vim9: can't call internal methods with objects
Christian Brabandt <cb@256bit.org>
parents: 34112
diff changeset
735 should not accept any arguments and must return a string.
5c1a025192ed patch 9.1.0148: Vim9: can't call internal methods with objects
Christian Brabandt <cb@256bit.org>
parents: 34112
diff changeset
736
5c1a025192ed patch 9.1.0148: Vim9: can't call internal methods with objects
Christian Brabandt <cb@256bit.org>
parents: 34112
diff changeset
737 *E1413*
5c1a025192ed patch 9.1.0148: Vim9: can't call internal methods with objects
Christian Brabandt <cb@256bit.org>
parents: 34112
diff changeset
738 A class method cannot be used as a builtin method.
31430
e5e95e8c78a7 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
739
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
740 Defining an interface ~
34056
c1074a41895e runtime(doc): add missing help tags
Christian Brabandt <cb@256bit.org>
parents: 34033
diff changeset
741 *Interface* *:interface* *:endinterface*
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
742 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
743 prefixed with `:export`: >
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 interface InterfaceName
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
746 endinterface
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
747
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
748 export interface InterfaceName
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
749 endinterface
31671
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
750 < *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
751 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
752 any initializer.
31671
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
753 *E1345*
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
754 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
755 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
756
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
757 interface HasSurface
33942
3bba09502b8d patch 9.0.2167: Vim9: not consistently using :var for declarations
Christian Brabandt <cb@256bit.org>
parents: 33931
diff changeset
758 var size: number
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
759 def Surface(): number
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
760 endinterface
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
761
31671
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
762 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
763 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
764 name, with a hint about what it provides.
31671
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
765 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
766 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
767 interface. *E1381*
31335
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
32004
a9b5ffbc0428 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31885
diff changeset
770 null object ~
a9b5ffbc0428 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31885
diff changeset
771
32061
b2412874362f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 32004
diff changeset
772 When a variable is declared to have the type of an object, but it is not
32004
a9b5ffbc0428 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31885
diff changeset
773 initialized, the value is null. When trying to use this null object Vim often
a9b5ffbc0428 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31885
diff changeset
774 does not know what class was supposed to be used. Vim then cannot check if
34098
8241df3e6dc1 runtime(doc): Improve the documentation for Vim9 classes (#13828)
Christian Brabandt <cb@256bit.org>
parents: 34057
diff changeset
775 a variable name is correct and you will get a "Using a null object" error,
33434
484543479bd7 runtime(doc): fix typos.
Christian Brabandt <cb@256bit.org>
parents: 33432
diff changeset
776 even when the variable name is invalid. *E1360* *E1362*
32004
a9b5ffbc0428 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31885
diff changeset
777
a9b5ffbc0428 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31885
diff changeset
778
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
779 Default constructor ~
33383
5c12ca4dcd45 patch 9.0.1950: Vim9: error codes spread out
Christian Brabandt <cb@256bit.org>
parents: 33379
diff changeset
780 *default-constructor*
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
781 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
782 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
783 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
784
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
785 class AutoNew
33942
3bba09502b8d patch 9.0.2167: Vim9: not consistently using :var for declarations
Christian Brabandt <cb@256bit.org>
parents: 33931
diff changeset
786 var name: string
3bba09502b8d patch 9.0.2167: Vim9: not consistently using :var for declarations
Christian Brabandt <cb@256bit.org>
parents: 33931
diff changeset
787 var age: number
3bba09502b8d patch 9.0.2167: Vim9: not consistently using :var for declarations
Christian Brabandt <cb@256bit.org>
parents: 33931
diff changeset
788 var gender: Gender
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
789 endclass
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
790
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
791 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
792
31441
e572ff386670 patch 9.0.1053: default constructor arguments are not optional
Bram Moolenaar <Bram@vim.org>
parents: 31430
diff changeset
793 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
794 enddef
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
795
31441
e572ff386670 patch 9.0.1053: default constructor arguments are not optional
Bram Moolenaar <Bram@vim.org>
parents: 31430
diff changeset
796 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
797 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
798 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
799 with default values: >
31430
e5e95e8c78a7 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
800
e5e95e8c78a7 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
801 class TextPosition
33942
3bba09502b8d patch 9.0.2167: Vim9: not consistently using :var for declarations
Christian Brabandt <cb@256bit.org>
parents: 33931
diff changeset
802 var lnum: number = 1
3bba09502b8d patch 9.0.2167: Vim9: not consistently using :var for declarations
Christian Brabandt <cb@256bit.org>
parents: 33931
diff changeset
803 var col: number = 1
31430
e5e95e8c78a7 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
804 endclass
e5e95e8c78a7 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
805
e5e95e8c78a7 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
806 If you want the constructor to have mandatory arguments, you need to write it
e5e95e8c78a7 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
807 yourself. For example, if for the AutoNew class above you insist on getting
e5e95e8c78a7 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
808 the name, you can define the constructor like this: >
e5e95e8c78a7 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
809
31441
e572ff386670 patch 9.0.1053: default constructor arguments are not optional
Bram Moolenaar <Bram@vim.org>
parents: 31430
diff changeset
810 def new(this.name, this.age = v:none, this.gender = v:none)
31430
e5e95e8c78a7 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
811 enddef
33891
56b32a00f325 runtime(doc): update todo items (#13631)
Christian Brabandt <cb@256bit.org>
parents: 33798
diff changeset
812 <
56b32a00f325 runtime(doc): update todo items (#13631)
Christian Brabandt <cb@256bit.org>
parents: 33798
diff changeset
813 When using the default new() method, if the order of the object variables in
56b32a00f325 runtime(doc): update todo items (#13631)
Christian Brabandt <cb@256bit.org>
parents: 33798
diff changeset
814 the class is changed later, then all the callers of the default new() method
34098
8241df3e6dc1 runtime(doc): Improve the documentation for Vim9 classes (#13828)
Christian Brabandt <cb@256bit.org>
parents: 34057
diff changeset
815 need to change. To avoid this, the new() method can be explicitly defined
33891
56b32a00f325 runtime(doc): update todo items (#13631)
Christian Brabandt <cb@256bit.org>
parents: 33798
diff changeset
816 without any arguments.
56b32a00f325 runtime(doc): update todo items (#13631)
Christian Brabandt <cb@256bit.org>
parents: 33798
diff changeset
817
56b32a00f325 runtime(doc): update todo items (#13631)
Christian Brabandt <cb@256bit.org>
parents: 33798
diff changeset
818 *E1328*
31441
e572ff386670 patch 9.0.1053: default constructor arguments are not optional
Bram Moolenaar <Bram@vim.org>
parents: 31430
diff changeset
819 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
820 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
821 way you only need to look in one place for the default values.
31430
e5e95e8c78a7 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
822
33738
2172872dfbcd patch 9.0.2096: Vim9: confusing usage of private
Christian Brabandt <cb@256bit.org>
parents: 33698
diff changeset
823 All object variables will be used in the default constructor, including
2172872dfbcd patch 9.0.2096: Vim9: confusing usage of private
Christian Brabandt <cb@256bit.org>
parents: 33698
diff changeset
824 protected access ones.
31671
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
825
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
826 If the class extends another one, the object variables of that class will come
31671
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
827 first.
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
828
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
829
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
830 Multiple constructors ~
34025
385c61388fc3 runtime(doc): add help tag multiple-constructors. (#13772)
Christian Brabandt <cb@256bit.org>
parents: 34012
diff changeset
831 *multiple-constructors*
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
832 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
833 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
834 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
835 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
836
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
837 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
838 enddef
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
839 ...
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
840 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
841 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
842 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
843
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
844 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
845 includes it: >
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
846
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
847 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
848 this.color = Color.black
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
849 enddef
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
850 ...
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
851 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
852 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
853 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
854
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
855 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
856 "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
857 constructor methods.
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
858
34112
0f2632b04cde patch 9.1.0020: Vim9: cannot compile all methods in a class
Christian Brabandt <cb@256bit.org>
parents: 34098
diff changeset
859 Compiling methods in a Class ~
0f2632b04cde patch 9.1.0020: Vim9: cannot compile all methods in a class
Christian Brabandt <cb@256bit.org>
parents: 34098
diff changeset
860 *class-compile*
0f2632b04cde patch 9.1.0020: Vim9: cannot compile all methods in a class
Christian Brabandt <cb@256bit.org>
parents: 34098
diff changeset
861 The |:defcompile| command can be used to compile all the class and object
0f2632b04cde patch 9.1.0020: Vim9: cannot compile all methods in a class
Christian Brabandt <cb@256bit.org>
parents: 34098
diff changeset
862 methods defined in a class: >
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
863
34112
0f2632b04cde patch 9.1.0020: Vim9: cannot compile all methods in a class
Christian Brabandt <cb@256bit.org>
parents: 34098
diff changeset
864 defcompile MyClass # Compile class "MyClass"
0f2632b04cde patch 9.1.0020: Vim9: cannot compile all methods in a class
Christian Brabandt <cb@256bit.org>
parents: 34098
diff changeset
865 defcompile # Compile the classes in the current script
0f2632b04cde patch 9.1.0020: Vim9: cannot compile all methods in a class
Christian Brabandt <cb@256bit.org>
parents: 34098
diff changeset
866 <
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
867 ==============================================================================
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
868
34056
c1074a41895e runtime(doc): add missing help tags
Christian Brabandt <cb@256bit.org>
parents: 34033
diff changeset
869 7. Type definition *typealias* *Vim9-type* *:type*
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
870
33931
050160b94f02 patch 9.0.2162: Vim9: type documentation out-dated
Christian Brabandt <cb@256bit.org>
parents: 33891
diff changeset
871 *E1393* *E1395* *E1396* *E1397* *E1398*
050160b94f02 patch 9.0.2162: Vim9: type documentation out-dated
Christian Brabandt <cb@256bit.org>
parents: 33891
diff changeset
872 A type definition is giving a name to a type specification. This is also
050160b94f02 patch 9.0.2162: Vim9: type documentation out-dated
Christian Brabandt <cb@256bit.org>
parents: 33891
diff changeset
873 known as a "type alias". The type alias can be used wherever a built-in type
050160b94f02 patch 9.0.2162: Vim9: type documentation out-dated
Christian Brabandt <cb@256bit.org>
parents: 33891
diff changeset
874 can be used. Example: >
33490
33593174288b runtime(doc): Update vim9class help (#13292)
Christian Brabandt <cb@256bit.org>
parents: 33434
diff changeset
875
33931
050160b94f02 patch 9.0.2162: Vim9: type documentation out-dated
Christian Brabandt <cb@256bit.org>
parents: 33891
diff changeset
876 type ListOfStrings = list<string>
050160b94f02 patch 9.0.2162: Vim9: type documentation out-dated
Christian Brabandt <cb@256bit.org>
parents: 33891
diff changeset
877 var s: ListOfStrings = ['a', 'b']
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
878
33931
050160b94f02 patch 9.0.2162: Vim9: type documentation out-dated
Christian Brabandt <cb@256bit.org>
parents: 33891
diff changeset
879 def ProcessStr(str: ListOfStrings): ListOfStrings
050160b94f02 patch 9.0.2162: Vim9: type documentation out-dated
Christian Brabandt <cb@256bit.org>
parents: 33891
diff changeset
880 return str
050160b94f02 patch 9.0.2162: Vim9: type documentation out-dated
Christian Brabandt <cb@256bit.org>
parents: 33891
diff changeset
881 enddef
050160b94f02 patch 9.0.2162: Vim9: type documentation out-dated
Christian Brabandt <cb@256bit.org>
parents: 33891
diff changeset
882 echo ProcessStr(s)
050160b94f02 patch 9.0.2162: Vim9: type documentation out-dated
Christian Brabandt <cb@256bit.org>
parents: 33891
diff changeset
883 <
050160b94f02 patch 9.0.2162: Vim9: type documentation out-dated
Christian Brabandt <cb@256bit.org>
parents: 33891
diff changeset
884 *E1394*
050160b94f02 patch 9.0.2162: Vim9: type documentation out-dated
Christian Brabandt <cb@256bit.org>
parents: 33891
diff changeset
885 A type alias name must start with an upper case character. Only existing
050160b94f02 patch 9.0.2162: Vim9: type documentation out-dated
Christian Brabandt <cb@256bit.org>
parents: 33891
diff changeset
886 types can be aliased.
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
887
33931
050160b94f02 patch 9.0.2162: Vim9: type documentation out-dated
Christian Brabandt <cb@256bit.org>
parents: 33891
diff changeset
888 *E1399*
050160b94f02 patch 9.0.2162: Vim9: type documentation out-dated
Christian Brabandt <cb@256bit.org>
parents: 33891
diff changeset
889 A type alias can be created only at the script level and not inside a
050160b94f02 patch 9.0.2162: Vim9: type documentation out-dated
Christian Brabandt <cb@256bit.org>
parents: 33891
diff changeset
890 function. A type alias can be exported and used across scripts.
050160b94f02 patch 9.0.2162: Vim9: type documentation out-dated
Christian Brabandt <cb@256bit.org>
parents: 33891
diff changeset
891
050160b94f02 patch 9.0.2162: Vim9: type documentation out-dated
Christian Brabandt <cb@256bit.org>
parents: 33891
diff changeset
892 *E1400* *E1401* *E1402* *E1403* *E1407*
050160b94f02 patch 9.0.2162: Vim9: type documentation out-dated
Christian Brabandt <cb@256bit.org>
parents: 33891
diff changeset
893 A type alias cannot be used as an expression. A type alias cannot be used in
050160b94f02 patch 9.0.2162: Vim9: type documentation out-dated
Christian Brabandt <cb@256bit.org>
parents: 33891
diff changeset
894 the left-hand-side of an assignment.
050160b94f02 patch 9.0.2162: Vim9: type documentation out-dated
Christian Brabandt <cb@256bit.org>
parents: 33891
diff changeset
895
050160b94f02 patch 9.0.2162: Vim9: type documentation out-dated
Christian Brabandt <cb@256bit.org>
parents: 33891
diff changeset
896 For a type alias name, the |typename()| function returns the type that is
050160b94f02 patch 9.0.2162: Vim9: type documentation out-dated
Christian Brabandt <cb@256bit.org>
parents: 33891
diff changeset
897 aliased: >
050160b94f02 patch 9.0.2162: Vim9: type documentation out-dated
Christian Brabandt <cb@256bit.org>
parents: 33891
diff changeset
898
050160b94f02 patch 9.0.2162: Vim9: type documentation out-dated
Christian Brabandt <cb@256bit.org>
parents: 33891
diff changeset
899 type ListOfStudents = list<dict<any>>
050160b94f02 patch 9.0.2162: Vim9: type documentation out-dated
Christian Brabandt <cb@256bit.org>
parents: 33891
diff changeset
900 echo typename(ListOfStudents)
050160b94f02 patch 9.0.2162: Vim9: type documentation out-dated
Christian Brabandt <cb@256bit.org>
parents: 33891
diff changeset
901 typealias<list<dict<any>>>
050160b94f02 patch 9.0.2162: Vim9: type documentation out-dated
Christian Brabandt <cb@256bit.org>
parents: 33891
diff changeset
902 <
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
903 ==============================================================================
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
904
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
905 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
906
34676
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
907 *enum* *E1418* *E1419* *E1420*
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
908 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
909
34676
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
910 :enum Color
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
911 White,
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
912 Red,
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
913 Green, Blue, Black
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
914 :endenum
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
915 <
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
916 *enumvalue* *E1422*
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
917 The enum values are separated by commas. More than one enum value can be
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
918 listed in a single line. The final enum value should not be followed by a
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
919 comma.
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
920
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
921 An enum value is accessed using the enum name followed by the value name: >
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
922
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
923 var a: Color = Color.Blue
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
924 <
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
925 Enums are treated as classes, where each enum value is essentially an instance
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
926 of that class. Unlike typical object instantiation with the |new()| method,
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
927 enum instances cannot be created this way.
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
928
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
929 An enum can only be defined in a |Vim9| script file. *E1414*
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
930 An enum cannot be defined inside a function.
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
931
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
932 *E1415*
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
933 An enum name must start with an uppercase letter. The name of an enum value
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
934 in an enum can start with an upper or lowercase letter.
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
935
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
936 *E1416*
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
937 An enum can implement an interface but cannot extend a class: >
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
938
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
939 enum MyEnum implements MyIntf
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
940 Value1,
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
941 Value2
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
942
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
943 def SomeMethod()
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
944 enddef
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
945 endenum
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
946 <
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
947 *enum-constructor*
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
948 The enum value objects in an enum are constructed like any other objects using
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
949 the |new()| method. Arguments can be passed to the enum constructor by
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
950 specifying them after the enum value name, just like calling a function. The
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
951 default constructor doesn't have any arguments.
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
952
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
953 *E1417*
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
954 An enum can contain class variables, class methods, object variables and
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
955 object methods. The methods in an enum cannot be |:abstract| methods.
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
956
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
957 The following example shows an enum with object variables and methods: >
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
958
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
959 vim9script
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
960 enum Planet
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
961 Earth(1, false),
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
962 Jupiter(95, true),
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
963 Saturn(146, true)
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
964
34676
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
965 var moons: number
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
966 var has_rings: bool
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
967 def GetMoons(): number
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
968 return this.moons
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
969 enddef
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
970 endenum
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
971 echo Planet.Jupiter.GetMoons()
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
972 echo Planet.Earth.has_rings
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
973 <
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
974 *E1421* *E1423* *E1424* *E1425*
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
975 Enums and their values are immutable. They cannot be modified after
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
976 declaration and cannot be utilized as numerical or string types.
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
977
34676
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
978 *enum-name*
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
979 Each enum value object has a "name" instance variable which contains the name
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
980 of the enum value. This is a readonly variable.
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
981
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
982 *enum-ordinal* *E1426*
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
983 Each enum value has an associated ordinal number starting with 0. The ordinal
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
984 number of an enum value can be accessed using the "ordinal" instance variable.
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
985 This is a readonly variable. Note that if the ordering of the enum values in
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
986 an enum is changed, then their ordinal values will also change.
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
987
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
988 *enum-values*
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
989 All the values in an enum can be accessed using the "values" class variable
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
990 which is a List of the enum objects. This is a readonly variable.
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
991
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
992 Example: >
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
993 enum Planet
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
994 Mercury,
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
995 Venus,
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
996 Earth
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
997 endenum
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
998
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
999 echo Planet.Mercury
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
1000 echo Planet.Venus.name
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
1001 echo Planet.Venus.ordinal
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
1002 for p in Planet.values
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
1003 # ...
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
1004 endfor
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
1005 <
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
1006 An enum is a class with class variables for the enum value objects and object
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
1007 variables for the enum value name and the enum value ordinal: >
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
1008
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
1009 enum Planet
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
1010 Mercury,
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
1011 Venus
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
1012 endenum
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
1013 <
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
1014 The above enum definition is equivalent to the following class definition: >
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
1015
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
1016 class Planet
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
1017 public static final Mercury: Planet = Planet.new('Mercury', 0)
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
1018 public static final Venus: Planet = Planet.new('Venus', 1)
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
1019
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
1020 public static const values: list<Planet> = [Planet.Mercury, Planet.Venus]
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
1021
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
1022 public const name: string
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
1023 public const ordinal: number
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
1024 endclass
5b25ec43f208 patch 9.1.0219: Vim9: No enum support
Christian Brabandt <cb@256bit.org>
parents: 34472
diff changeset
1025 <
31335
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
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1028 9. Rationale
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1029
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1030 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
1031 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
1032 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
1033 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
1034
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1035 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
1036 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
1037 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
1038 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
1039 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
1040 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
1041 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
1042
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1043 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
1044 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
1045 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
1046 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
1047 understand.
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1048
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1049 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
1050 - Keep it simple.
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1051 - 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
1052 - 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
1053 - 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
1054 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
1055 - Keep it consistent.
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1056 - 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
1057
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1058
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1059 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
1060
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1061 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
1062 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
1063 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
1064 disadvantage.
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1065
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1066 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
1067 "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
1068 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
1069
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1070 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
1071 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
1072 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
1073
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1074
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1075 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
1076
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1077 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
1078 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
1079 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
1080 constructor.
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1081
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1082 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
1083 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
1084 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
1085 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
1086 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
1087 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
1088 returning.
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1089
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1090 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
1091 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
1092 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
1093 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
1094
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1095
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1096 No overloading of methods ~
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1097
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1098 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
1099 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
1100 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
1101 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
1102 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
1103
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1104
31671
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
1105 Single inheritance and interfaces ~
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
1106
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
1107 Some languages support multiple inheritance. Although that can be useful in
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
1108 some cases, it makes the rules of how a class works quite complicated.
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
1109 Instead, using interfaces to declare what is supported is much simpler. The
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
1110 very popular Java language does it this way, and it should be good enough for
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
1111 Vim. The "keep it simple" rule applies here.
31671
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
1112
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
1113 Explicitly declaring that a class supports an interface makes it easy to see
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
1114 what a class is intended for. It also makes it possible to do proper type
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
1115 checking. When an interface is changed any class that declares to implement
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
1116 it will be checked if that change was also changed. The mechanism to assume a
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
1117 class implements an interface just because the methods happen to match is
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
1118 brittle and leads to obscure problems, let's not do that.
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
1119
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
1120
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
1121 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
1122
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
1123 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
1124 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
1125 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
1126 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
1127
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1128 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
1129 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
1130 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
1131 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
1132 inconsistent.
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1133
34098
8241df3e6dc1 runtime(doc): Improve the documentation for Vim9 classes (#13828)
Christian Brabandt <cb@256bit.org>
parents: 34057
diff changeset
1134 For |Vim9| classes the "this." prefix is always used for declared methods 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
1135 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
1136 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
1137 which aren't.
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1138
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1139
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
1140 Using class variables ~
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1141
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
1142 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
1143 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
1144 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
1145 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
1146 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
1147
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
1148 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
1149 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
1150 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
1151 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
1152
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1153
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
1154 Declaring object and class variables ~
31671
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
1155
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
1156 The main choice is whether to use "var" as with variable declarations.
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
1157 TypeScript does not use it: >
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
1158 class Point {
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
1159 x: number;
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
1160 y = 0;
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
1161 }
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
1162
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
1163 Following that Vim object variables could be declared like this: >
31671
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
1164 class Point
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
1165 this.x: number
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
1166 this.y = 0
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
1167 endclass
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
1168
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
1169 Some users pointed out that this looks more like an assignment than a
34098
8241df3e6dc1 runtime(doc): Improve the documentation for Vim9 classes (#13828)
Christian Brabandt <cb@256bit.org>
parents: 34057
diff changeset
1170 declaration. Adding "var" and omitting "this." changes that: >
31671
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
1171 class Point
33942
3bba09502b8d patch 9.0.2167: Vim9: not consistently using :var for declarations
Christian Brabandt <cb@256bit.org>
parents: 33931
diff changeset
1172 var x: number
3bba09502b8d patch 9.0.2167: Vim9: not consistently using :var for declarations
Christian Brabandt <cb@256bit.org>
parents: 33931
diff changeset
1173 var y = 0
31671
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
1174 endclass
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
1175
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
1176 We also need to be able to declare class variables using the "static" keyword.
31671
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
1177 There we can also choose to leave out "var": >
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
1178 class Point
33942
3bba09502b8d patch 9.0.2167: Vim9: not consistently using :var for declarations
Christian Brabandt <cb@256bit.org>
parents: 33931
diff changeset
1179 var x: number
31671
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
1180 static count = 0
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
1181 endclass
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
1182
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
1183 Or do use it, before "static": >
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
1184 class Point
33942
3bba09502b8d patch 9.0.2167: Vim9: not consistently using :var for declarations
Christian Brabandt <cb@256bit.org>
parents: 33931
diff changeset
1185 var x: number
31671
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
1186 var static count = 0
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
1187 endclass
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
1188
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
1189 Or after "static": >
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
1190 class Point
33942
3bba09502b8d patch 9.0.2167: Vim9: not consistently using :var for declarations
Christian Brabandt <cb@256bit.org>
parents: 33931
diff changeset
1191 var x: number
31671
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
1192 static var count = 0
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
1193 endclass
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
1194
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
1195 This is more in line with "static def Func()".
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
1196
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
1197 There is no clear preference whether to use "var" or not. The two main
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
1198 reasons to leave it out are:
33942
3bba09502b8d patch 9.0.2167: Vim9: not consistently using :var for declarations
Christian Brabandt <cb@256bit.org>
parents: 33931
diff changeset
1199 1. TypeScript and other popular languages do not use it.
31671
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
1200 2. Less clutter.
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
1201
33942
3bba09502b8d patch 9.0.2167: Vim9: not consistently using :var for declarations
Christian Brabandt <cb@256bit.org>
parents: 33931
diff changeset
1202 However, it is more common for languages to reuse their general variable and
3bba09502b8d patch 9.0.2167: Vim9: not consistently using :var for declarations
Christian Brabandt <cb@256bit.org>
parents: 33931
diff changeset
1203 function declaration syntax for class/object variables and methods. Vim9 also
3bba09502b8d patch 9.0.2167: Vim9: not consistently using :var for declarations
Christian Brabandt <cb@256bit.org>
parents: 33931
diff changeset
1204 reuses the general function declaration syntax for methods. So, for the sake
3bba09502b8d patch 9.0.2167: Vim9: not consistently using :var for declarations
Christian Brabandt <cb@256bit.org>
parents: 33931
diff changeset
1205 of consistency, we require "var" in these declarations.
3bba09502b8d patch 9.0.2167: Vim9: not consistently using :var for declarations
Christian Brabandt <cb@256bit.org>
parents: 33931
diff changeset
1206
31671
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
1207
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1208 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
1209
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1210 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
1211 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
1212 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
1213 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
1214
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1215 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
1216 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
1217 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
1218 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
1219
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1220
33738
2172872dfbcd patch 9.0.2096: Vim9: confusing usage of private
Christian Brabandt <cb@256bit.org>
parents: 33698
diff changeset
1221 Vim9class access modes ~
2172872dfbcd patch 9.0.2096: Vim9: confusing usage of private
Christian Brabandt <cb@256bit.org>
parents: 33698
diff changeset
1222 *vim9-access-modes*
2172872dfbcd patch 9.0.2096: Vim9: confusing usage of private
Christian Brabandt <cb@256bit.org>
parents: 33698
diff changeset
1223 The variable access modes, and their meaning, supported by Vim9class are
2172872dfbcd patch 9.0.2096: Vim9: confusing usage of private
Christian Brabandt <cb@256bit.org>
parents: 33698
diff changeset
1224 |public-variable| read and write from anywhere
2172872dfbcd patch 9.0.2096: Vim9: confusing usage of private
Christian Brabandt <cb@256bit.org>
parents: 33698
diff changeset
1225 |read-only-variable| read from anywhere, write from inside the
2172872dfbcd patch 9.0.2096: Vim9: confusing usage of private
Christian Brabandt <cb@256bit.org>
parents: 33698
diff changeset
1226 class and sub-classes
2172872dfbcd patch 9.0.2096: Vim9: confusing usage of private
Christian Brabandt <cb@256bit.org>
parents: 33698
diff changeset
1227 |protected-variable| read and write from inside the class and
2172872dfbcd patch 9.0.2096: Vim9: confusing usage of private
Christian Brabandt <cb@256bit.org>
parents: 33698
diff changeset
1228 sub-classes
2172872dfbcd patch 9.0.2096: Vim9: confusing usage of private
Christian Brabandt <cb@256bit.org>
parents: 33698
diff changeset
1229
2172872dfbcd patch 9.0.2096: Vim9: confusing usage of private
Christian Brabandt <cb@256bit.org>
parents: 33698
diff changeset
1230 The method access modes are similar, but without the read-only mode.
2172872dfbcd patch 9.0.2096: Vim9: confusing usage of private
Christian Brabandt <cb@256bit.org>
parents: 33698
diff changeset
1231
2172872dfbcd patch 9.0.2096: Vim9: confusing usage of private
Christian Brabandt <cb@256bit.org>
parents: 33698
diff changeset
1232
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
1233 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
1234
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
1235 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
1236 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
1237 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
1238 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
1239 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
1240 objects.
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1241
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
1242 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
1243 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
1244 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
1245 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
1246 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
1247
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1248
33738
2172872dfbcd patch 9.0.2096: Vim9: confusing usage of private
Christian Brabandt <cb@256bit.org>
parents: 33698
diff changeset
1249 Making object variables protected with an underscore ~
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1250
33738
2172872dfbcd patch 9.0.2096: Vim9: confusing usage of private
Christian Brabandt <cb@256bit.org>
parents: 33698
diff changeset
1251 When an object variable is protected, it can only be read and changed inside
2172872dfbcd patch 9.0.2096: Vim9: confusing usage of private
Christian Brabandt <cb@256bit.org>
parents: 33698
diff changeset
1252 the class (and in sub-classes), then it cannot be used outside of the class.
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1253 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
1254 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
1255
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
1256 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
1257 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
1258 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
1259 to find and change.
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1260
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1261 The other way around is much harder: you can easily prepend an underscore to
33738
2172872dfbcd patch 9.0.2096: Vim9: confusing usage of private
Christian Brabandt <cb@256bit.org>
parents: 33698
diff changeset
1262 the object variable inside the class to make it protected, but any usage
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
1263 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
1264 "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
1265 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
1266
33738
2172872dfbcd patch 9.0.2096: Vim9: confusing usage of private
Christian Brabandt <cb@256bit.org>
parents: 33698
diff changeset
1267 An alternative would have been using the "protected" keyword, just like
2172872dfbcd patch 9.0.2096: Vim9: confusing usage of private
Christian Brabandt <cb@256bit.org>
parents: 33698
diff changeset
1268 "public" changes the access in the other direction. Well, that's just to
2172872dfbcd patch 9.0.2096: Vim9: confusing usage of private
Christian Brabandt <cb@256bit.org>
parents: 33698
diff changeset
1269 reduce the number of keywords.
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1270
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1271
33738
2172872dfbcd patch 9.0.2096: Vim9: confusing usage of private
Christian Brabandt <cb@256bit.org>
parents: 33698
diff changeset
1272 No private object variables ~
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1273
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
1274 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
1275 The most known is "protected", and the meaning varies from language to
33738
2172872dfbcd patch 9.0.2096: Vim9: confusing usage of private
Christian Brabandt <cb@256bit.org>
parents: 33698
diff changeset
1276 language. Others are "shared", "private", "package" and even "friend".
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1277
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1278 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
1279 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
1280 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
1281
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1282 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
1283 small team working on it. Complex rules then only make it more complicated,
32294
b2e8663e6dcc Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 32061
diff changeset
1284 the extra safety provided by the rules isn't really needed. Let's just keep
b2e8663e6dcc Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 32061
diff changeset
1285 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
1286
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1287
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1288 ==============================================================================
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1289
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1290 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
1291
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1292 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
1293 the restrictions?
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1294
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1295 Thoughts:
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1296 - 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
1297 - 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
1298 - 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
1299
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1300 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
1301 - For testing: Mock mechanism
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1302
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1303 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
1304 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
1305 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
1306 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
1307
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1308
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1309 vim:tw=78:ts=8:noet:ft=help:norl: