Skip to content

Commit

Permalink
Merge pull request #1952 from h-east/update-vim9class
Browse files Browse the repository at this point in the history
Update vim9class.{txt,jax}
  • Loading branch information
h-east authored Feb 19, 2025
2 parents 5822acd + dcda94c commit cd11275
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 44 deletions.
36 changes: 14 additions & 22 deletions doc/vim9class.jax
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
*vim9class.txt* For Vim バージョン 9.1. Last change: 2024 Dec 29
*vim9class.txt* For Vim バージョン 9.1. Last change: 2025 Feb 16


VIMリファレンスマニュアル by Bram Moolenaar
Expand Down Expand Up @@ -53,7 +53,6 @@ script や従来の関数では使用できない。
クラス階層は単一の継承になる。それ以外の場合は、必要に応じてインターフェイスを
使用すること。


クラスのモデリング ~

好きな方法でクラスをモデル化できる。何を構築しているのかを念頭に置き、現実世界
Expand Down Expand Up @@ -123,7 +122,6 @@ new() メソッドを使用して、このクラスからオブジェクトを
クラス名を式として使用することはできない。クラス名は、代入の左辺では使用できな
い。


オブジェクト変数の書き込みアクセス ~
*read-only-variable*
では、オブジェクト変数を直接変更してみよう: >
Expand Down Expand Up @@ -267,6 +265,9 @@ new() を使用して変数をパブリックにするこの方法をまとめ
new() メソッドを定義するときは、戻り値の型を指定しないこと。常にクラスのオブ
ジェクトを返す。

new() メソッドは、"_new()" を使用して protected メソッドにすることができる。こ
れは、シングルトン設計パターンをサポートするために使用できる。

*E1386*
オブジェクトメソッドを呼び出すときは、メソッド名の前にオブジェクト変数名を付け
る必要がある。クラス名を使用してオブジェクトメソッドを呼び出すことはできない。
Expand Down Expand Up @@ -620,13 +621,15 @@ Shape, Square および Triangle を使用した上記の例は、オブジェ
extends ClassName
implements InterfaceName, OtherInterface
specifies SomeInterface
< *E1355* *E1369*
<
"specifies" 機能は現在実装されていない。

*E1355* *E1369*
各変数とメソッドの名前は 1 回だけ使用できる。同じ名前で異なる型の引数を持つメ
ソッドを定義することはできない。パブリックと protected のメンバー変数を同じ名
前で使用することはできない。スーパークラスで使用したオブジェクト変数名を子クラ
スで再利用することはできない。


オブジェクト変数の初期化 ~

変数の型がクラスで明示的に指定されていない場合、クラス定義時に "any" に設定さ
Expand Down Expand Up @@ -681,13 +684,12 @@ Shape, Square および Triangle を使用した上記の例は、オブジェ
のみ使用できる *E1350* 。複数のインターフェイスをコンマで区切って指定できる。
各インターフェイス名は 1 回だけ出現できる。 *E1351*


インターフェイスを定義するクラス ~
*specifies*
クラスは、名前付きインターフェイスを使用して、そのインターフェイス、オブジェク
ト変数およびメソッドを宣言できる。これにより、多くの言語、特に Java で頻繁に行
われる、インターフェイスを個別に指定する必要がなくなった。

TODO: 現時点では実装されていない。

クラス内の項目 ~
*E1318* *E1325* *E1388*
Expand Down Expand Up @@ -728,24 +730,24 @@ Shape, Square および Triangle を使用した上記の例は、オブジェ
使用できる。オブジェクトは、これらの組み込み関数と同じ名前のメソッドを実装し
て、オブジェクト固有の値を返すことができる。

*E1412*
*E1412*
以下の組み込みメソッドがサポートされている:
*object-empty()*
*object-empty()*
empty() |empty()| 関数によって呼び出され、オブジェクトが空かどうかを確認
する。このメソッドが存在しない場合は、true が返される。このメソッ
ドは引数を受け入れず、真偽値を返す必要がある。
*object-len()*
*object-len()*
len() |len()| 関数によって呼び出され、オブジェクトの長さを返す。このメ
ソッドがクラスにない場合はエラーが発生し、ゼロが返される。このメ
ソッドは引数を受け入れず、数値を返す必要がある。
*object-string()*
*object-string()*
string() |string()| 関数によって呼び出され、オブジェクトのテキスト表現を取
得する。オブジェクトに対する |:echo| コマンドでも使用される。この
メソッドがクラスにない場合は、組み込みのデフォルトのテキスト表現
が使用される。このメソッドは引数を受け入れず、文字列を返す必要が
ある。

*E1413*
*E1413*
クラスメソッドを組み込みメソッドとして使用することはできない。

インターフェイスの定義 ~
Expand Down Expand Up @@ -777,7 +779,6 @@ Shape, Square および Triangle を使用した上記の例は、オブジェ
インターフェイスは他のインターフェイスを "implement" することはできないが、他
のインターフェイスを "extend" することはできる。 *E1381*


null オブジェクト ~

変数がオブジェクトの型を持つように宣言されているが初期化されていない場合、値は
Expand All @@ -786,7 +787,6 @@ null になる。この null オブジェクトを使用しようとすると、
正しいかどうかをチェックできず、変数名が無効な場合でも "Using a null object"
エラーが発生する。 *E1360* *E1362*


デフォルトコンストラクタ ~
*default-constructor*
new() メソッドを使用せずにクラスを定義した場合は、自動的に new() メソッドが定
Expand Down Expand Up @@ -1100,7 +1100,6 @@ TypeScript などの他の言語では、"constructor()" 等の特定の名前
トは正しい選択であるように思えた。これを new() と呼び出すことで、呼び出し元と
呼び出されるメソッドの関係が明確になる。


コンストラクタのオーバーロードを行わない ~

Vim script では、旧来と |Vim9| の両スクリプトとも、メソッドのオーバーロードは
Expand All @@ -1119,15 +1118,13 @@ Vim script では、旧来と |Vim9| の両スクリプトとも、メソッド
のコンストラクタが使用されているかを確認するのが非常に簡単になる。引数の型も適
切に確認できる。


メソッドのオーバーロードを行わない ~

コンストラクタの場合と同じ理由である。多くの場合、引数の型が明らかではないた
め、実際にどのメソッドが呼び出されているかを把握することが困難になる。メソッド
に別の名前を付けるだけで、型チェックによって意図したとおりに動作することが確認
される。これにより、実際には必要のないポリモーフィズムが排除される。


単一の継承とインターフェイス ~

一部の言語は多重継承をサポートしている。これは場合によっては便利だが、クラスの
Expand All @@ -1142,7 +1139,6 @@ Vim script では、旧来と |Vim9| の両スクリプトとも、メソッド
たま一致したからといって、クラスがインターフェイスを実装しているとみなすメカニ
ズムは脆弱で、分かりにくい問題を引き起こすため、やめておこう。


あらゆる場所で "this.variable" を使用する ~

さまざまなプログラミング言語のオブジェクト変数には、多くの場合、場所に応じてさ
Expand All @@ -1160,7 +1156,6 @@ Vim script では、旧来と |Vim9| の両スクリプトとも、メソッド
使用される。シンプルで一貫性がある。クラス内部のコードを見る時にも、どの変数参
照がオブジェクト変数で、どれがそうでないかが直接明らかになる。


クラス変数の使用 ~

"static variable" を使ってクラス変数を宣言するのはよくあることで、特に目新しい
Expand All @@ -1173,7 +1168,6 @@ TypeScript は、クラス内でもクラス変数名の前にクラス名を付
問題がある: クラス名はかなり長くなり、かなりのスペースが必要になることと、クラ
ス名が変更されたときに、これらの場所もすべて変更する必要があることだ。


オブジェクト変数とクラス変数の宣言 ~

主な選択肢は、変数宣言のように "var" を使うかどうかである。TypeScript では使用
Expand Down Expand Up @@ -1226,7 +1220,6 @@ Vim のオブジェクト変数は以下のように宣言できる: >
を再利用している。そのため、一貫性を保つために、これらの宣言では "var" を必要
とする。


"ClassName.new()" を使用してオブジェクトを構築する ~

多くの言語では、オブジェクトの作成に "new" 演算子を使用するが、コンストラクタ
Expand Down Expand Up @@ -1291,7 +1284,6 @@ Vim9class でサポートされる変数アクセスモードとその意味は
"protected" キーワードを使用することもできる。まぁ、これはキーワードの数を減ら
すためなのだが。


オブジェクト変数に private はない ~

一部の言語では、オブジェクト変数へのアクセスを制御するいくつかの方法が提供され
Expand Down
36 changes: 14 additions & 22 deletions en/vim9class.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
*vim9class.txt* For Vim version 9.1. Last change: 2024 Dec 29
*vim9class.txt* For Vim version 9.1. Last change: 2025 Feb 16


VIM REFERENCE MANUAL by Bram Moolenaar
Expand Down Expand Up @@ -52,7 +52,6 @@ An interface is used to specify properties of an object:
The class hierarchy allows for single inheritance. Otherwise interfaces are
to be used where needed.


Class modeling ~

You can model classes any way you like. Keep in mind what you are building,
Expand Down Expand Up @@ -122,7 +121,6 @@ using the object name followed by a dot following by the member: >
A class name cannot be used as an expression. A class name cannot be used in
the left-hand-side of an assignment.


Object variable write access ~
*read-only-variable*
Now try to change an object variable directly: >
Expand Down Expand Up @@ -272,6 +270,9 @@ no need to call "super()" or "new()" on the parent.
When defining the new() method the return type should not be specified. It
always returns an object of the class.

The new() method can be made a protected method by using "_new()". This can
be used to support the singleton design pattern.

*E1386*
When invoking an object method, the method name should be preceded by the
object variable name. An object method cannot be invoked using the class
Expand Down Expand Up @@ -623,13 +624,15 @@ once. They can appear in any order, although this order is recommended: >
extends ClassName
implements InterfaceName, OtherInterface
specifies SomeInterface
< *E1355* *E1369*
<
The "specifies" feature is currently not implemented.

*E1355* *E1369*
Each variable and method name can be used only once. It is not possible to
define a method with the same name and different type of arguments. It is not
possible to use a public and protected member variable with the same name. An
object variable name used in a super class cannot be reused in a child class.


Object Variable Initialization ~

If the type of a variable is not explicitly specified in a class, then it is
Expand Down Expand Up @@ -686,13 +689,12 @@ A class can implement one or more interfaces. The "implements" keyword can
only appear once *E1350* . Multiple interfaces can be specified, separated by
commas. Each interface name can appear only once. *E1351*


A class defining an interface ~
*specifies*
A class can declare its interface, the object variables and methods, with a
named interface. This avoids the need for separately specifying the
interface, which is often done in many languages, especially Java.

TODO: This is currently not implemented.

Items in a class ~
*E1318* *E1325* *E1388*
Expand Down Expand Up @@ -733,25 +735,25 @@ Some of the builtin functions like |empty()|, |len()| and |string()| can be
used with an object. An object can implement a method with the same name as
these builtin functions to return an object-specific value.

*E1412*
*E1412*
The following builtin methods are supported:
*object-empty()*
*object-empty()*
empty() Invoked by the |empty()| function to check whether an object is
empty. If this method is missing, then true is returned. This
method should not accept any arguments and must return a boolean.
*object-len()*
*object-len()*
len() Invoked by the |len()| function to return the length of an
object. If this method is missing in the class, then an error is
given and zero is returned. This method should not accept any
arguments and must return a number.
*object-string()*
*object-string()*
string() Invoked by the |string()| function to get a textual
representation of an object. Also used by the |:echo| command
for an object. If this method is missing in the class, then a
built-in default textual representation is used. This method
should not accept any arguments and must return a string.

*E1413*
*E1413*
A class method cannot be used as a builtin method.

Defining an interface ~
Expand Down Expand Up @@ -783,7 +785,6 @@ An interface can only be defined in a |Vim9| script file. *E1342*
An interface cannot "implement" another interface but it can "extend" another
interface. *E1381*


null object ~

When a variable is declared to have the type of an object, but it is not
Expand All @@ -792,7 +793,6 @@ does not know what class was supposed to be used. Vim then cannot check if
a variable name is correct and you will get a "Using a null object" error,
even when the variable name is invalid. *E1360* *E1362*


Default constructor ~
*default-constructor*
In case you define a class without a new() method, one will be automatically
Expand Down Expand Up @@ -1115,7 +1115,6 @@ For |Vim9| script using the same method name for all constructors seemed like
the right choice, and by calling it new() the relation between the caller and
the method being called is obvious.


No overloading of the constructor ~

In Vim script, both legacy and |Vim9| script, there is no overloading of
Expand All @@ -1136,7 +1135,6 @@ That way multiple constructors with different arguments are possible, while it
is very easy to see which constructor is being used. And the type of
arguments can be properly checked.


No overloading of methods ~

Same reasoning as for the constructor: It is often not obvious what type
Expand All @@ -1145,7 +1143,6 @@ actually being called. Better just give the methods a different name, then
type checking will make sure it works as you intended. This rules out
polymorphism, which we don't really need anyway.


Single inheritance and interfaces ~

Some languages support multiple inheritance. Although that can be useful in
Expand All @@ -1161,7 +1158,6 @@ it will be checked if that change was also changed. The mechanism to assume a
class implements an interface just because the methods happen to match is
brittle and leads to obscure problems, let's not do that.


Using "this.variable" everywhere ~

The object variables in various programming languages can often be accessed in
Expand All @@ -1180,7 +1176,6 @@ variables. Simple and consistent. When looking at the code inside a class
it's also directly clear which variable references are object variables and
which aren't.


Using class variables ~

Using "static variable" to declare a class variable is very common, nothing
Expand All @@ -1194,7 +1189,6 @@ the class. This has two problems: The class name can be rather long, taking
up quite a bit of space, and when the class is renamed all these places need
to be changed too.


Declaring object and class variables ~

The main choice is whether to use "var" as with variable declarations.
Expand Down Expand Up @@ -1248,7 +1242,6 @@ function declaration syntax for class/object variables and methods. Vim9 also
reuses the general function declaration syntax for methods. So, for the sake
of consistency, we require "var" in these declarations.


Using "ClassName.new()" to construct an object ~

Many languages use the "new" operator to create an object, which is actually
Expand Down Expand Up @@ -1312,7 +1305,6 @@ An alternative would have been using the "protected" keyword, just like
"public" changes the access in the other direction. Well, that's just to
reduce the number of keywords.


No private object variables ~

Some languages provide several ways to control access to object variables.
Expand Down

0 comments on commit cd11275

Please sign in to comment.