annotate runtime/doc/vim9class.txt @ 33217:499ba27ba0f6 v9.0.1885

patch 9.0.1885: Vim9: no support for abstract methods Commit: https://github.com/vim/vim/commit/7bcd25cad3e9d5c9e25c7ae2bde67285c26f73cd Author: Yegappan Lakshmanan <yegappan@yahoo.com> Date: Fri Sep 8 19:27:51 2023 +0200 patch 9.0.1885: Vim9: no support for abstract methods Problem: Vim9: no support for abstract methods Solution: Add support for defining abstract methods in an abstract class closes: #13044 closes: #13046 Signed-off-by: Christian Brabandt <cb@256bit.org> Co-authored-by: Yegappan Lakshmanan <yegappan@yahoo.com>
author Christian Brabandt <cb@256bit.org>
date Fri, 08 Sep 2023 19:45:03 +0200
parents d42927c6e556
children 0c3553cfe22e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
32294
b2e8663e6dcc Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 32061
diff changeset
1 *vim9class.txt* For Vim version 9.0. Last change: 2023 Mar 22
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
3
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
4 VIM REFERENCE MANUAL by Bram Moolenaar
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
5
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
6
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
7 NOTE - This is not finished yet, anything can still change! - NOTE
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
8
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
9
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
10 Vim9 classes, objects, interfaces, types and enums.
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
11
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
12 1. Overview |Vim9-class-overview|
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
13 2. A simple class |Vim9-simple-class|
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
14 3. Class members and functions |Vim9-class-member|
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
15 4. Using an abstract class |Vim9-abstract-class|
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
16 5. Using an interface |Vim9-using-interface|
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
17 6. More class details |Vim9-class|
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
18 7. Type definition |Vim9-type|
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
19 8. Enum |Vim9-enum|
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
20
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
21 9. Rationale
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
22 10. To be done later
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
23
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
24 ==============================================================================
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
25
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
26 1. Overview *Vim9-class-overview*
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
27
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
28 The fancy term is "object-oriented programming". You can find lots of study
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
29 material on this subject. Here we document what |Vim9| script provides,
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
30 assuming you know the basics already. Added are helpful hints about how to
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
31 use this functionality effectively.
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
32
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
33 The basic item is an object:
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
34 - An object stores state. It contains one or more variables that can each
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
35 have a value.
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
36 - An object provides functions that use and manipulate its state. These
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
37 functions are invoked "on the object", which is what sets it apart from the
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
38 traditional separation of data and code that manipulates the data.
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
39 - An object has a well defined interface, with typed member variables and
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
40 member functions.
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
41 - 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
42 This does not change at runtime, it is not dynamic.
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
43
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
44 An object can only be created by a class. A class provides:
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
45 - A new() method, the constructor, which returns an object for the class.
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
46 This method is invoked on the class name: MyClass.new().
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
47 - State shared by all objects of the class: class variables (class members).
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
48 - A hierarchy of classes, with super-classes and sub-classes, inheritance.
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
49
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
50 An interface is used to specify properties of an object:
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
51 - An object can declare several interfaces that it implements.
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
52 - Different objects implementing the same interface can be used the same way.
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
53
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
54 The class hierarchy allows for single inheritance. Otherwise interfaces are
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
55 to be used where needed.
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
56
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
57
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
58 Class modeling ~
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
59
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
60 You can model classes any way you like. Keep in mind what you are building,
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
61 don't try to model the real world. This can be confusing, especially because
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
62 teachers use real-world objects to explain class relations and you might think
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
63 your model should therefore reflect the real world. It doesn't! The model
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
64 should match your purpose.
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
65
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
66 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
67 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
68 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
69 square is a rectangle or that a rectangle is a square. It doesn't matter.
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
70
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
71
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
72 ==============================================================================
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
73
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
74 2. A simple class *Vim9-simple-class*
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
75
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
76 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
77 below for how to do this more efficiently): >
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
78
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
79 class TextPosition
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
80 this.lnum: number
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
81 this.col: number
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
82
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
83 def new(lnum: number, col: number)
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
84 this.lnum = lnum
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
85 this.col = col
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
86 enddef
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
87
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
88 def SetLnum(lnum: number)
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
89 this.lnum = lnum
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
90 enddef
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
91
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
92 def SetCol(col: number)
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
93 this.col = col
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
94 enddef
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
95
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
96 def SetPosition(lnum: number, col: number)
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
97 this.lnum = lnum
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
98 this.col = col
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
99 enddef
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
100 endclass
31430
e5e95e8c78a7 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
101 < *object* *Object*
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
102 You can create an object from this class with the new() method: >
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
103
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
104 var pos = TextPosition.new(1, 1)
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
105
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
106 The object members "lnum" and "col" can be accessed directly: >
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
107
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
108 echo $'The text position is ({pos.lnum}, {pos.col})'
31430
e5e95e8c78a7 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
109 < *E1317* *E1327*
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
110 If you have been using other object-oriented languages you will notice that
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
111 in Vim the object members are consistently referred to with the "this."
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
112 prefix. This is different from languages like Java and TypeScript. The
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
113 naming convention makes the object members easy to spot. Also, when a
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
114 variable does not have the "this." prefix you know it is not an object member.
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
115
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
116
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
117 Member write access ~
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
118
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
119 Now try to change an object member directly: >
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
120
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
121 pos.lnum = 9
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
122 < *E1335*
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
123 This will give you an error! That is because by default object members can be
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
124 read but not set. That's why the TextPosition class provides a method for it: >
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
125
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
126 pos.SetLnum(9)
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
127
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
128 Allowing to read but not set an object member is the most common and safest
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
129 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
130 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
131 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
132 the closest valid value.
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
133 *:public* *E1331*
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
134 If you don't care about side effects and want to allow the object member to be
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
135 changed at any time, you can make it public: >
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
136
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
137 public this.lnum: number
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
138 public this.col: number
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
139
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
140 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
141 "pos.lnum" directly above will no longer give an error.
31579
7d68a90cbf5c Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
142 *E1334*
7d68a90cbf5c Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
143 If you try to set an object member that doesn't exist you get an error: >
7d68a90cbf5c Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
144 pos.other = 9
7d68a90cbf5c Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
145 < E1334: Object member not found: other ~
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
146
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
147
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
148 Private members ~
31579
7d68a90cbf5c Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
149 *E1332* *E1333*
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
150 On the other hand, if you do not want the object members to be read directly,
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
151 you can make them private. This is done by prefixing an underscore to the
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
152 name: >
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
153
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
154 this._lnum: number
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
155 this._col number
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
156
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
157 Now you need to provide methods to get the value of the private members.
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
158 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
159 "Get": >
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
160
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
161 def GetLnum(): number
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
162 return this._lnum
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
163 enddef
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
164
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
165 def GetCol() number
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
166 return this._col
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
167 enddef
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
168
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
169 This example isn't very useful, the members might as well have been public.
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
170 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
171 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
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 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
175 return this._lineCount
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
176 endif
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
177 return this._lnum
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
178 enddef
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
179
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
180
33025
1d18c7fe609f patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents: 32960
diff changeset
181 Private methods ~
1d18c7fe609f patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents: 32960
diff changeset
182 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
183 same class and not used from outside the class, then you can make them
1d18c7fe609f patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents: 32960
diff changeset
184 private. This is done by prefixing the method name with an underscore: >
1d18c7fe609f patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents: 32960
diff changeset
185
1d18c7fe609f patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents: 32960
diff changeset
186 class SomeClass
1d18c7fe609f patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents: 32960
diff changeset
187 def _Foo(): number
1d18c7fe609f patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents: 32960
diff changeset
188 return 10
1d18c7fe609f patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents: 32960
diff changeset
189 enddef
1d18c7fe609f patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents: 32960
diff changeset
190 def Bar(): number
1d18c7fe609f patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents: 32960
diff changeset
191 return this._Foo()
1d18c7fe609f patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents: 32960
diff changeset
192 enddef
1d18c7fe609f patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents: 32960
diff changeset
193 endclass
1d18c7fe609f patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents: 32960
diff changeset
194 <
1d18c7fe609f patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents: 32960
diff changeset
195 Accessing a private method outside the class will result in an error (using
1d18c7fe609f patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents: 32960
diff changeset
196 the above class): >
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 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
199 a._Foo()
1d18c7fe609f patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents: 32960
diff changeset
200 <
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
201 Simplifying the new() method ~
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
202
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
203 Many constructors take values for the object members. Thus you very often see
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
204 this pattern: >
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
205
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
206 class SomeClass
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
207 this.lnum: number
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
208 this.col: number
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
209
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
210 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
211 this.lnum = lnum
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
212 this.col = col
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
213 enddef
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
214 endclass
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
215
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
216 Not only is this text you need to write, it also has the type of each member
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
217 twice. Since this is so common a shorter way to write new() is provided: >
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
218
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
219 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
220 enddef
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
221
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
222 The semantics are easy to understand: Providing the object member name,
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
223 including "this.", as the argument to new() means the value provided in the
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
224 new() call is assigned to that object member. This mechanism comes from the
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
225 Dart language.
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
226
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
227 Putting together this way of using new() and making the members public results
32294
b2e8663e6dcc Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 32061
diff changeset
228 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
229
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
230 class TextPosition
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
231 public this.lnum: number
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
232 public this.col: number
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
233
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
234 def new(this.lnum, this.col)
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
235 enddef
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
236
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
237 def SetPosition(lnum: number, col: number)
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
238 this.lnum = lnum
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
239 this.col = col
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
240 enddef
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
241 endclass
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
242
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
243 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
244 1. Memory is allocated and cleared. All values are zero/false/empty.
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
245 2. For each declared member that has an initializer, the expression is
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
246 evaluated and assigned to the member. This happens in the sequence the
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
247 members are declared in the class.
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
248 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
249 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
250
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
251 If the class extends a parent class, the same thing happens. In the second
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
252 step the members of the parent class are done first. There is no need to call
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
253 "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
254
32903
54c01bb98b8e patch 9.0.1760: vim9 class problem with new() constructor
Christian Brabandt <cb@256bit.org>
parents: 32294
diff changeset
255 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
256 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
257
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
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
260 3. class members and functions *Vim9-class-member*
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
261
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
262 *:static* *E1337* *E1338*
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
263 Class members are declared with "static". They are used by the name without a
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
264 prefix: >
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
265
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
266 class OtherThing
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
267 this.size: number
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
268 static totalSize: number
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
269
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
270 def new(this.size)
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
271 totalSize += this.size
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
272 enddef
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
273 endclass
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
274 < *E1340* *E1341*
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
275 Since the name is used as-is, shadowing the name by a function argument name
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
276 or local variable name is not allowed.
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
277
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
278 Just like object members the access can be made private by using an underscore
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
279 as the first character in the name, and it can be made public by prefixing
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
280 "public": >
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
281
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
282 class OtherThing
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
283 static total: number # anybody can read, only class can write
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
284 static _sum: number # only class can read and write
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
285 public static result: number # anybody can read and write
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
286 endclass
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-function*
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
289 Class functions are also declared with "static". They have no access to
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
290 object members, they cannot use the "this" keyword. >
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 class OtherThing
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
293 this.size: number
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
294 static totalSize: number
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
295
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
296 # Clear the total size and return the value it had before.
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
297 static def ClearTotalSize(): number
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
298 var prev = totalSize
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
299 totalSize = 0
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
300 return prev
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
301 enddef
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
302 endclass
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
303
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
304 Inside the class the function can be called by name directly, outside the
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
305 class the class name must be prefixed: `OtherThing.ClearTotalSize()`.
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
306
33025
1d18c7fe609f patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents: 32960
diff changeset
307 Just like object methods the access can be made private by using an underscore
1d18c7fe609f patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents: 32960
diff changeset
308 as the first character in the method name: >
1d18c7fe609f patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents: 32960
diff changeset
309
1d18c7fe609f patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents: 32960
diff changeset
310 class OtherThing
1d18c7fe609f patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents: 32960
diff changeset
311 static def _Foo()
1d18c7fe609f patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents: 32960
diff changeset
312 echo "Foo"
1d18c7fe609f patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents: 32960
diff changeset
313 enddef
1d18c7fe609f patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents: 32960
diff changeset
314 def Bar()
1d18c7fe609f patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents: 32960
diff changeset
315 OtherThing._Foo()
1d18c7fe609f patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents: 32960
diff changeset
316 enddef
1d18c7fe609f patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents: 32960
diff changeset
317 endclass
33068
d42927c6e556 patch 9.0.1821: Vim9 constructors are always static
Christian Brabandt <cb@256bit.org>
parents: 33025
diff changeset
318 <
d42927c6e556 patch 9.0.1821: Vim9 constructors are always static
Christian Brabandt <cb@256bit.org>
parents: 33025
diff changeset
319 *E1370*
d42927c6e556 patch 9.0.1821: Vim9 constructors are always static
Christian Brabandt <cb@256bit.org>
parents: 33025
diff changeset
320 Note that constructors cannot be declared as "static", because they always
d42927c6e556 patch 9.0.1821: Vim9 constructors are always static
Christian Brabandt <cb@256bit.org>
parents: 33025
diff changeset
321 are.
33025
1d18c7fe609f patch 9.0.1804: Vim9: no support for private object methods
Christian Brabandt <cb@256bit.org>
parents: 32960
diff changeset
322
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
323 ==============================================================================
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
324
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
325 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
326
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
327 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
328 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
329 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
330 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
331 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
332
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
333 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
334 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
335 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
336 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
337
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
338 abstract class Shape
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
339 this.color = Color.Black
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
340 this.thickness = 10
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
341 endclass
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
342
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
343 class Square extends Shape
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
344 this.size: number
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
345
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
346 def new(this.size)
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
347 enddef
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
348 endclass
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
349
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
350 class Triangle extends Shape
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
351 this.base: number
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
352 this.height: number
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
353
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
354 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
355 enddef
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
356 endclass
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
357 <
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
358 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
359 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
360
33217
499ba27ba0f6 patch 9.0.1885: Vim9: no support for abstract methods
Christian Brabandt <cb@256bit.org>
parents: 33068
diff changeset
361 *abstract-method*
499ba27ba0f6 patch 9.0.1885: Vim9: no support for abstract methods
Christian Brabandt <cb@256bit.org>
parents: 33068
diff changeset
362 An abstract method can be defined in an abstract class by using the "abstract"
499ba27ba0f6 patch 9.0.1885: Vim9: no support for abstract methods
Christian Brabandt <cb@256bit.org>
parents: 33068
diff changeset
363 prefix when defining the function: >
499ba27ba0f6 patch 9.0.1885: Vim9: no support for abstract methods
Christian Brabandt <cb@256bit.org>
parents: 33068
diff changeset
364
499ba27ba0f6 patch 9.0.1885: Vim9: no support for abstract methods
Christian Brabandt <cb@256bit.org>
parents: 33068
diff changeset
365 abstract class Shape
499ba27ba0f6 patch 9.0.1885: Vim9: no support for abstract methods
Christian Brabandt <cb@256bit.org>
parents: 33068
diff changeset
366 abstract def Draw()
499ba27ba0f6 patch 9.0.1885: Vim9: no support for abstract methods
Christian Brabandt <cb@256bit.org>
parents: 33068
diff changeset
367 endclass
499ba27ba0f6 patch 9.0.1885: Vim9: no support for abstract methods
Christian Brabandt <cb@256bit.org>
parents: 33068
diff changeset
368
499ba27ba0f6 patch 9.0.1885: Vim9: no support for abstract methods
Christian Brabandt <cb@256bit.org>
parents: 33068
diff changeset
369 A class extending the abstract class must implement all the abstract methods.
499ba27ba0f6 patch 9.0.1885: Vim9: no support for abstract methods
Christian Brabandt <cb@256bit.org>
parents: 33068
diff changeset
370 Class methods in an abstract class can also be abstract methods.
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
371
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
372 ==============================================================================
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
373
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
374 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
375
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
376 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
377 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
378 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
379 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
380
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
381 abstract class Shape
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
382 this.color = Color.Black
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
383 this.thickness = 10
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
384 endclass
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
385
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
386 interface HasSurface
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
387 def Surface(): number
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
388 endinterface
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
389
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
390 class Square extends Shape implements HasSurface
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
391 this.size: number
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
392
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
393 def new(this.size)
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
394 enddef
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
395
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
396 def Surface(): number
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
397 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
398 enddef
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
399 endclass
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
400
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
401 class Triangle extends Shape implements HasSurface
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
402 this.base: number
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
403 this.height: number
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
404
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
405 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
406 enddef
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
407
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
408 def Surface(): number
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
409 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
410 enddef
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
411 endclass
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
412
31671
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
413 If a class declares to implement an interface, all the items specified in the
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
414 interface must appear in the class, with the same types. *E1348* *E1349*
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
415
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
416 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
417
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
418 var shapes: list<HasSurface> = [
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
419 Square.new(12),
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
420 Triangle.new(8, 15),
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
421 ]
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
422 for shape in shapes
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
423 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
424 endfor
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
425
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
426
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
427 ==============================================================================
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
428
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
429 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
430
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
431 Defining a class ~
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
432 *:class* *:endclass* *:abstract*
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
433 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
434 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
435
31430
e5e95e8c78a7 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
436 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
437 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
438
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
439 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
440 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
441 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
442
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
443 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
444 these variants: >
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
445
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
446 class ClassName
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
447 endclass
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
448
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
449 export class ClassName
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
450 endclass
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
451
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
452 abstract class ClassName
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
453 endclass
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
454
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
455 export abstract class ClassName
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
456 endclass
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
457 <
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
458 *E1314*
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
459 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
460 That avoids clashing with builtin types.
31430
e5e95e8c78a7 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
461 *E1315*
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
462 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
463 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
464 extends ClassName
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
465 implements InterfaceName, OtherInterface
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
466 specifies SomeInterface
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
467 < *E1355*
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
468 Each member and function name can be used only once. It is not possible to
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
469 define a function with the same name and different type of arguments.
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
470
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
471
32960
d5c05e15cf81 patch 9.0.1780: Vim9 type not defined during object creation
Christian Brabandt <cb@256bit.org>
parents: 32903
diff changeset
472 Member Initialization ~
d5c05e15cf81 patch 9.0.1780: Vim9 type not defined during object creation
Christian Brabandt <cb@256bit.org>
parents: 32903
diff changeset
473 If the type of a member is not explicitly specified in a class, then it is set
d5c05e15cf81 patch 9.0.1780: Vim9 type not defined during object creation
Christian Brabandt <cb@256bit.org>
parents: 32903
diff changeset
474 to "any" during class definition. When an object is instantiated from the
d5c05e15cf81 patch 9.0.1780: Vim9 type not defined during object creation
Christian Brabandt <cb@256bit.org>
parents: 32903
diff changeset
475 class, then the type of the member is set.
d5c05e15cf81 patch 9.0.1780: Vim9 type not defined during object creation
Christian Brabandt <cb@256bit.org>
parents: 32903
diff changeset
476
d5c05e15cf81 patch 9.0.1780: Vim9 type not defined during object creation
Christian Brabandt <cb@256bit.org>
parents: 32903
diff changeset
477
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
478 Extending a class ~
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
479 *extends*
31671
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
480 A class can extend one other class. *E1352* *E1353* *E1354*
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
481 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
482
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
483 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
484 is called the "child class".
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
485
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
486 Object members from the base class are all taken over by the child class. It
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
487 is not possible to override them (unlike some other languages).
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
488
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
489 *E1356* *E1357* *E1358*
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
490 Object methods of the base class can be overruled. The signature (arguments,
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
491 argument types and return type) must be exactly the same. The method of the
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
492 base class can be called by prefixing "super.".
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
493
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
494 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
495
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
496 Class functions, including functions starting with "new", can be overruled,
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
497 like with object methods. The function on the base class can be called by
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
498 prefixing the name of the class (for class functions) or "super.".
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
499
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
500 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
501 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
502 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
503 call that method from every constructor().
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
504
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
505 If the base class did not specify a new() function then one was automatically
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
506 created. This function will not be taken over by the child class. The child
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
507 class can define its own new() function, or, if there isn't one, a new()
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
508 function will be added automatically.
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
509
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
510
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
511 A class implementing an interface ~
31671
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
512 *implements* *E1346* *E1347*
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
513 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
514 only appear once *E1350* . Multiple interfaces can be specified, separated by
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
515 commas. Each interface name can appear only once. *E1351*
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
516
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
517
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
518 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
519 *specifies*
31396
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
520 A class can declare its interface, the object members 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
521 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
522 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
523
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
524
31430
e5e95e8c78a7 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
525 Items in a class ~
e5e95e8c78a7 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
526 *E1318* *E1325* *E1326*
32061
b2412874362f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 32004
diff changeset
527 Inside a class, in between `:class` and `:endclass`, these items can appear:
31430
e5e95e8c78a7 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
528 - An object member declaration: >
32004
a9b5ffbc0428 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31885
diff changeset
529 this._memberName: memberType
a9b5ffbc0428 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31885
diff changeset
530 this.memberName: memberType
31430
e5e95e8c78a7 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
531 public this.memberName: memberType
e5e95e8c78a7 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
532 - A constructor method: >
32004
a9b5ffbc0428 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31885
diff changeset
533 def new(arguments)
a9b5ffbc0428 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31885
diff changeset
534 def newName(arguments)
31430
e5e95e8c78a7 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
535 - An object method: >
32004
a9b5ffbc0428 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31885
diff changeset
536 def SomeMethod(arguments)
31579
7d68a90cbf5c Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
537 < *E1329*
7d68a90cbf5c Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
538 For the object member the type must be specified. The best way is to do this
7d68a90cbf5c Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
539 explicitly with ": {type}". For simple types you can also use an initializer,
7d68a90cbf5c Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
540 such as "= 123", and Vim will see that the type is a number. Avoid doing this
7d68a90cbf5c Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
541 for more complex types and when the type will be incomplete. For example: >
7d68a90cbf5c Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
542 this.nameList = []
7d68a90cbf5c Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
543 This specifies a list, but the item type is unknown. Better use: >
7d68a90cbf5c Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
544 this.nameList: list<string>
7d68a90cbf5c Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
545 The initialization isn't needed, the list is empty by default.
7d68a90cbf5c Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
546 *E1330*
7d68a90cbf5c Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31441
diff changeset
547 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
548
e5e95e8c78a7 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
549
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
550 Defining an interface ~
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
551 *:interface* *:endinterface*
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
552 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
553 prefixed with `:export`: >
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
554
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
555 interface InterfaceName
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
556 endinterface
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
557
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
558 export interface InterfaceName
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
559 endinterface
31671
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
560 < *E1344*
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
561 An interface can declare object members, just like in a class but without any
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
562 initializer.
31671
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
563 *E1345*
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
564 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
565 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
566
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
567 interface HasSurface
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
568 this.size: number
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
569 def Surface(): number
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
570 endinterface
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
571
31671
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
572 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
573 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
574 name, with a hint about what it provides.
31671
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
575 An interface can only be defined in a |Vim9| script file. *E1342*
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
576
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
577
32004
a9b5ffbc0428 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31885
diff changeset
578 null object ~
a9b5ffbc0428 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31885
diff changeset
579
32061
b2412874362f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 32004
diff changeset
580 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
581 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
582 does not know what class was supposed to be used. Vim then cannot check if
a9b5ffbc0428 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31885
diff changeset
583 a member name is correct and you will get an "Using a null object" error,
32294
b2e8663e6dcc Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 32061
diff changeset
584 even when the member name is invalid. *E1360* *E1362* *E1363*
32004
a9b5ffbc0428 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31885
diff changeset
585
a9b5ffbc0428 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31885
diff changeset
586
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
587 Default constructor ~
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
588
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
589 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
590 defined. This default constructor will have arguments for all the object
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
591 members, in the order they were specified. Thus if your class looks like: >
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
592
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
593 class AutoNew
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
594 this.name: string
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
595 this.age: number
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
596 this.gender: Gender
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
597 endclass
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
598
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
599 Then The default constructor will be: >
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
600
31441
e572ff386670 patch 9.0.1053: default constructor arguments are not optional
Bram Moolenaar <Bram@vim.org>
parents: 31430
diff changeset
601 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
602 enddef
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
603
31441
e572ff386670 patch 9.0.1053: default constructor arguments are not optional
Bram Moolenaar <Bram@vim.org>
parents: 31430
diff changeset
604 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
605 call `new()` without any arguments. No assignment will happen and the default
e572ff386670 patch 9.0.1053: default constructor arguments are not optional
Bram Moolenaar <Bram@vim.org>
parents: 31430
diff changeset
606 value for the object members will be used. This is a more useful example,
e572ff386670 patch 9.0.1053: default constructor arguments are not optional
Bram Moolenaar <Bram@vim.org>
parents: 31430
diff changeset
607 with default values: >
31430
e5e95e8c78a7 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
608
e5e95e8c78a7 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
609 class TextPosition
e5e95e8c78a7 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
610 this.lnum: number = 1
e5e95e8c78a7 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
611 this.col: number = 1
e5e95e8c78a7 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
612 endclass
e5e95e8c78a7 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
613
e5e95e8c78a7 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
614 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
615 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
616 the name, you can define the constructor like this: >
e5e95e8c78a7 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
617
31441
e572ff386670 patch 9.0.1053: default constructor arguments are not optional
Bram Moolenaar <Bram@vim.org>
parents: 31430
diff changeset
618 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
619 enddef
31441
e572ff386670 patch 9.0.1053: default constructor arguments are not optional
Bram Moolenaar <Bram@vim.org>
parents: 31430
diff changeset
620 < *E1328*
e572ff386670 patch 9.0.1053: default constructor arguments are not optional
Bram Moolenaar <Bram@vim.org>
parents: 31430
diff changeset
621 Note that you cannot use another default value than "v:none" here. If you
e572ff386670 patch 9.0.1053: default constructor arguments are not optional
Bram Moolenaar <Bram@vim.org>
parents: 31430
diff changeset
622 want to initialize the object members, do it where they are declared. This
e572ff386670 patch 9.0.1053: default constructor arguments are not optional
Bram Moolenaar <Bram@vim.org>
parents: 31430
diff changeset
623 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
624
31671
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
625 All object members will be used in the default constructor, also private
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
626 access ones.
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
627
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
628 If the class extends another one, the object members of that class will come
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
629 first.
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
630
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
631
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
632 Multiple constructors ~
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
633
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
634 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
635 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
636 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
637 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
638
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
639 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
640 enddef
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
641 ...
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
642 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
643 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
644 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
645
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
646 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
647 includes it: >
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
648
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
649 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
650 this.color = Color.black
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
651 enddef
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
652 ...
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
653 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
654 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
655 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
656
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
657 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
658 "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
659 constructor methods.
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
660
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
661
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
662 ==============================================================================
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
663
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
664 7. Type definition *Vim9-type* *:type*
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
665
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
666 A type definition is giving a name to a type specification. For Example: >
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
667
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
668 :type ListOfStrings list<string>
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
669
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
670 TODO: more explanation
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
671
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
672
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
673 ==============================================================================
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
674
31885
cc751d944b7e Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 31671
diff changeset
675 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
676
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
677 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
678
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
679 :enum Color
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
680 White
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
681 Red
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
682 Green
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
683 Blue
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
684 Black
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
685 :endenum
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
686
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
687 TODO: more explanation
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
688
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
689
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
690 ==============================================================================
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
691
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
692 9. Rationale
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
693
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
694 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
695 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
696 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
697 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
698
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
699 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
700 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
701 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
702 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
703 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
704 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
705 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
706
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
707 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
708 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
709 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
710 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
711 understand.
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
712
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
713 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
714 - Keep it simple.
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
715 - 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
716 - 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
717 - 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
718 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
719 - Keep it consistent.
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
720 - 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
721
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
722
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
723 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
724
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
725 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
726 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
727 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
728 disadvantage.
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
729
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
730 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
731 "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
732 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
733
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
734 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
735 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
736 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
737
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
738
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
739 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
740
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
741 In Vim script, both legacy and |Vim9| script, there is no overloading of
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
742 functions. That means it is not possible to use the same function name with
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
743 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
744 constructor.
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
745
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
746 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
747 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
748 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
749 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
750 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
751 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
752 returning.
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
753
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
754 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
755 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
756 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
757 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
758
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
759
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
760 No overloading of methods ~
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
761
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
762 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
763 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
764 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
765 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
766 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
767
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
768
31671
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
769 Single inheritance and interfaces ~
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
770
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
771 Some languages support multiple inheritance. Although that can be useful in
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
772 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
773 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
774 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
775 Vim. The "keep it simple" rule applies here.
31671
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
776
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
777 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
778 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
779 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
780 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
781 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
782 brittle and leads to obscure problems, let's not do that.
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
783
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
784
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
785 Using "this.member" everywhere ~
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
786
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
787 The object members in various programming languages can often be accessed in
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
788 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
789 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
790 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
791
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
792 A very common issue is that in the constructor the arguments use the same name
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
793 as the object member. Then for these members "this." needs to be prefixed in
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
794 the body, while for other members this is not needed and often omitted. This
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
795 leads to a mix of members with and without "this.", which is inconsistent.
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
796
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
797 For |Vim9| classes the "this." prefix is always used. Also for declaring the
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
798 members. Simple and consistent. When looking at the code inside a class it's
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
799 also directly clear which variable references are object members and which
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
800 aren't.
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
801
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
802
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
803 Using class members ~
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
804
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
805 Using "static member" to declare a class member is very common, nothing new
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
806 here. In |Vim9| script these can be accessed directly by their name. Very
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
807 much like how a script-local variable can be used in a function. Since object
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
808 members are always accessed with "this." prepended, it's also quickly clear
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
809 what kind of member it is.
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
810
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
811 TypeScript prepends the class name before the class member, also inside the
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
812 class. This has two problems: The class name can be rather long, taking up
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
813 quite a bit of space, and when the class is renamed all these places need to
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
814 be changed too.
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
815
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
816
31671
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
817 Declaring object and class members ~
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
818
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
819 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
820 TypeScript does not use it: >
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
821 class Point {
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
822 x: number;
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
823 y = 0;
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
824 }
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
825
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
826 Following that Vim object members could be declared like this: >
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
827 class Point
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
828 this.x: number
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
829 this.y = 0
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
830 endclass
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
831
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
832 Some users pointed out that this looks more like an assignment than a
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
833 declaration. Adding "var" changes that: >
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
834 class Point
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
835 var this.x: number
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
836 var this.y = 0
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
837 endclass
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
838
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
839 We also need to be able to declare class members using the "static" keyword.
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
840 There we can also choose to leave out "var": >
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
841 class Point
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
842 var this.x: number
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
843 static count = 0
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
844 endclass
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
845
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
846 Or do use it, before "static": >
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
847 class Point
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
848 var this.x: number
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
849 var static count = 0
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
850 endclass
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
851
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
852 Or after "static": >
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
853 class Point
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
854 var this.x: number
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
855 static var count = 0
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
856 endclass
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
857
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
858 This is more in line with "static def Func()".
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
859
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
860 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
861 reasons to leave it out are:
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
862 1. TypeScript, Java and other popular languages do not use it.
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
863 2. Less clutter.
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
864
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31579
diff changeset
865
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
866 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
867
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
868 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
869 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
870 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
871 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
872
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
873 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
874 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
875 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
876 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
877
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
878
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
879 Default read access to object members ~
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
880
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
881 Some users will remark that the access rules for object members are
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
882 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
883 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
884 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
885 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
886 objects.
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
887
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
888 When adding object members one usually doesn't think much about this, just get
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
889 the type right. And normally the values are set in the new() method.
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
890 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
891 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
892 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
893
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
894
31396
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 31335
diff changeset
895 Making object members private with an underscore ~
31335
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
896
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
897 When an object member is private, it can only be read and changed inside the
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
898 class (and in sub-classes), then it cannot be used outside of the class.
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
899 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
900 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
901
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
902 In case you change your mind and want to make the object member accessible
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
903 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
904 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
905 to find and change.
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
906
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
907 The other way around is much harder: you can easily prepend an underscore to
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
908 the object member inside the class to make it private, but any usage elsewhere
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
909 you will have to track down and change. You may have to make it a "set"
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
910 method call. This reflects the real world problem that taking away access
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
911 requires work to be done for all places where that access exists.
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
912
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
913 An alternative would have been using the "private" keyword, just like "public"
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
914 changes the access in the other direction. Well, that's just to reduce the
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
915 number of keywords.
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
916
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
917
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
918 No protected object members ~
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
919
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
920 Some languages provide several ways to control access to object members. The
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
921 most known is "protected", and the meaning varies from language to language.
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
922 Others are "shared", "private" and even "friend".
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
923
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
924 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
925 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
926 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
927
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
928 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
929 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
930 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
931 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
932
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
933
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
934 ==============================================================================
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
935
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
936 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
937
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
938 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
939 the restrictions?
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
940
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
941 Thoughts:
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
942 - 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
943 - 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
944 - 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
945
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
946 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
947 - For testing: Mock mechanism
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
948
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
949 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
950 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
951 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
952 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
953
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
954
5acc0d2cf4f7 patch 9.0.1001: classes are not documented or implemented yet
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
955 vim:tw=78:ts=8:noet:ft=help:norl: