Skip to content

Commit 6412555

Browse files
authored
Merge pull request #2184 from h-east/update-vim9
Update vim9.{txt,jax}
2 parents fd5fd74 + 3c65653 commit 6412555

File tree

2 files changed

+288
-14
lines changed

2 files changed

+288
-14
lines changed

doc/vim9.jax

Lines changed: 141 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*vim9.txt* For Vim バージョン 9.1. Last change: 2025 Apr 27
1+
*vim9.txt* For Vim バージョン 9.1. Last change: 2025 Jul 21
22

33
VIMリファレンスマニュアル by Bram Moolenaar
44

@@ -14,10 +14,11 @@ script の新しい文法と機能について書かれています。
1414
2. 変更点 |vim9-differences|
1515
3. 新しいスタイルの関数 |fast-functions|
1616
4. 型 |vim9-types|
17-
5. 名前空間、Import と Export |vim9script|
18-
6. クラスとインターフェイス |vim9-classes|
17+
5. ジェネリック関数 |generic-functions|
18+
6. 名前空間、Import と Export |vim9script|
19+
7. クラスとインターフェイス |vim9-classes|
1920

20-
9. 理論的根拠 |vim9-rationale|
21+
8. 理論的根拠 |vim9-rationale|
2122

2223
==============================================================================
2324

@@ -1895,7 +1896,140 @@ NOTE: ジョブなどの特殊な変数はデフォルトで null 値になり
18951896

18961897
==============================================================================
18971898

1898-
5. 名前空間、Import と Export
1899+
*generic-functions*
1900+
5. ジェネリック関数
1901+
1902+
ジェネリック関数は、引数と戻り値の型チェックを維持しながら、同じ関数を異なる型
1903+
の引数で使用できる。これにより、型安全性とコードの再利用性が確保される。
1904+
1905+
宣言~
1906+
*generic-function-declaration*
1907+
*E1553* *E1554* *E1559*
1908+
ジェネリック関数の型パラメータは、関数名の直後に山括弧 "<" と ">" で宣言され
1909+
る。複数の型名はコンマで区切られる: >
1910+
1911+
def[!] {funcname}<{type} [, {types}]>([arguments])[: {return-type}]
1912+
{function body}
1913+
enddef
1914+
<
1915+
これらの型パラメータは、関数のシグネチャと本体内の他の型と同じように使用でき
1916+
る。例: >
1917+
1918+
def MyFunc<T, A, B>(param1: T): T
1919+
var f: A
1920+
var x = param1
1921+
return x
1922+
enddef
1923+
<
1924+
*type-variable-naming* *E1552*
1925+
型変数には大文字 1 文字を使用するのが慣例である (例: T、A、X)。ただし、より長
1926+
い名前も使用できる。名前は必ず大文字で始まる必要がある。
1927+
1928+
*E1558* *E1560*
1929+
関数は、ジェネリック関数または通常の関数のいずれかとして宣言および使用する必要
1930+
がある。両方としてはできない。
1931+
1932+
*E1561*
1933+
型変数名は、クラス名、型エイリアス、列挙名、関数名、他の型変数名など、他の定義
1934+
済みの名前と競合してはならない。
1935+
1936+
ジェネリック関数の呼び出し~
1937+
*generic-function-call*
1938+
ジェネリック関数を呼び出すには、関数名と引数リストの間に "<" と ">" で具体的な
1939+
型を指定する: >
1940+
1941+
MyFunc<number, string, list<number>>()
1942+
<
1943+
*E1555* *E1556* *E1557*
1944+
ジェネリック関数を呼び出す際に指定する具体的な型の数は、関数内の型変数の数と一
1945+
致する必要がある。空の型リストは許可されない。Vim9 の任意の型 (|vim9-types|)
1946+
は、ジェネリック関数内の具体的な型として使用できる。
1947+
1948+
関数名と "<" の間、または ">" と開始の "(" の間には空白を入れることはできない。
1949+
1950+
ジェネリック関数は通常の関数と同様に export および import できる。|:export|
1951+
よび |:import| を参照。
1952+
1953+
ジェネリック関数は、別の通常関数またはジェネリック関数内で定義できる。
1954+
1955+
ジェネリック型における型変数の参照~
1956+
1957+
ジェネリック型では、具体的な型の代わりに型変数を使用できる。これは、辞書のリス
1958+
トやリストの辞書のような複雑なデータ構造に便利である。例: >
1959+
1960+
vim9script
1961+
1962+
def Flatten<T>(x: list<list<T>>): list<T>
1963+
var result: list<T> = []
1964+
for inner in x
1965+
result += inner
1966+
endfor
1967+
return result
1968+
enddef
1969+
1970+
echo Flatten<number>([[1, 2], [3]])
1971+
<
1972+
1973+
ジェネリッククラスメソッド~
1974+
1975+
Vim9 クラスメソッドはジェネリック関数にすることができる: >
1976+
1977+
class A
1978+
def Foo<X, Y>()
1979+
enddef
1980+
endclass
1981+
var a = A.new()
1982+
a.Foo<number, string>()
1983+
<
1984+
*E1432* *E1433* *E1434*
1985+
基底クラスのジェネリッククラスメソッドは、子クラスのジェネリックメソッドでオー
1986+
バーライドできる。型変数の数は両方のメソッド間で一致している必要がある。具象ク
1987+
ラスメソッドをジェネリックメソッドでオーバーライドすることはできない。逆もまた
1988+
同様である。
1989+
1990+
ジェネリック関数参照~
1991+
1992+
関数参照 (|Funcref|) はジェネリック関数として使用できる。これにより、特定の型
1993+
を操作する関数のファクトリーを作成できる: >
1994+
1995+
vim9script
1996+
1997+
def MakeEcho<T>(): func(T): T
1998+
return (x: T): T => x
1999+
enddef
2000+
2001+
var EchoNumber = MakeEcho<number>()
2002+
echo EchoNumber(123)
2003+
2004+
var EchoString = MakeEcho<string>()
2005+
echo EchoString('abc')
2006+
<
2007+
ジェネリック関数のコンパイルと逆アセンブル~
2008+
2009+
|:defcompile| コマンドは、具体的な型の特定のリストを持つジェネリック関数をコン
2010+
パイルするために使用できる: >
2011+
2012+
defcompile MyFunc<number, list<number>, dict<string>>
2013+
<
2014+
|:disassemble| コマンドを使用すると、ジェネリック関数に対して生成された命令を
2015+
一覧表示できる: >
2016+
2017+
disassemble MyFunc<string, dict<string>>
2018+
disassemble MyFunc<number, list<blob>>
2019+
<
2020+
制限事項と今後の課題~
2021+
2022+
現在、Vim は以下をサポートしていない:
2023+
- 型変数の型推論: ジェネリック関数を呼び出す際は、すべての型を明示的に指定
2024+
する必要がある。
2025+
- 型制約: 型変数を特定のクラスまたはインターフェイスに制限することはできな
2026+
い (例: `T extends SomeInterface`)。
2027+
- デフォルト型引数: 型パラメータが明示的に指定されていない場合に、デフォル
2028+
トの型を提供する。
2029+
2030+
==============================================================================
2031+
2032+
6. 名前空間、Import と Export
18992033
*vim9script* *vim9-export* *vim9-import*
19002034

19012035
Vim9 script は、import されるように書くことができます。これは、いくつかの項目
@@ -2178,7 +2312,7 @@ import したオートロードスクリプトを使用するマッピングを
21782312
21792313
==============================================================================
21802314

2181-
6. クラスとインターフェイス *vim9-classes*
2315+
7. クラスとインターフェイス *vim9-classes*
21822316

21832317
旧来のスクリプトでは、関数であるメンバーを追加することで、辞書を一種のオブジェ
21842318
クトとして使用することができます。しかし、これは非常に非効率的で、すべてのオブ
@@ -2192,7 +2326,7 @@ import したオートロードスクリプトを使用するマッピングを
21922326

21932327
==============================================================================
21942328

2195-
9. 理論的根拠 *vim9-rationale*
2329+
8. 理論的根拠 *vim9-rationale*
21962330

21972331
:def コマンド ~
21982332

en/vim9.txt

Lines changed: 147 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*vim9.txt* For Vim version 9.1. Last change: 2025 Apr 27
1+
*vim9.txt* For Vim version 9.1. Last change: 2025 Jul 21
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -15,10 +15,11 @@ features in Vim9 script.
1515
2. Differences |vim9-differences|
1616
3. New style functions |fast-functions|
1717
4. Types |vim9-types|
18-
5. Namespace, Import and Export |vim9script|
19-
6. Classes and interfaces |vim9-classes|
18+
5. Generic functions |generic-functions|
19+
6. Namespace, Import and Export |vim9script|
20+
7. Classes and interfaces |vim9-classes|
2021

21-
9. Rationale |vim9-rationale|
22+
8. Rationale |vim9-rationale|
2223

2324
==============================================================================
2425

@@ -1895,7 +1896,146 @@ corresponding empty value.
18951896

18961897
==============================================================================
18971898

1898-
5. Namespace, Import and Export
1899+
*generic-functions*
1900+
5. Generic functions
1901+
1902+
A generic function allows using the same function with different type
1903+
arguments, while retaining type checking for arguments and the return value.
1904+
This provides type safety and code reusability.
1905+
1906+
Declaration~
1907+
*generic-function-declaration*
1908+
*E1553* *E1554* *E1559*
1909+
The type parameters for a generic function are declared in angle brackets "<"
1910+
and ">" directly after the function name. Multiple type names are separated
1911+
by commas: >
1912+
1913+
def[!] {funcname}<{type} [, {types}]>([arguments])[: {return-type}]
1914+
{function body}
1915+
enddef
1916+
<
1917+
These type parameters can then be used like any other type within the function
1918+
signature and body. Example: >
1919+
1920+
def MyFunc<T, A, B>(param1: T): T
1921+
var f: A
1922+
var x = param1
1923+
return x
1924+
enddef
1925+
<
1926+
*type-variable-naming* *E1552*
1927+
The convention is to use a single uppercase letter for a type variable (e.g.,
1928+
T, A, X), although longer names are allowed. The name must start with an
1929+
uppercase letter.
1930+
1931+
*E1558* *E1560*
1932+
A function must be declared and used either as generic or as a regular
1933+
function - but not both.
1934+
1935+
*E1561*
1936+
A type variable name must not conflict with other defined names, such as class
1937+
names, type aliases, enum names, function names or other type variable names.
1938+
1939+
Calling a generic function~
1940+
*generic-function-call*
1941+
To call a generic function, specify the concrete types in "<" and ">"
1942+
between the function name and the argument list: >
1943+
1944+
MyFunc<number, string, list<number>>()
1945+
<
1946+
*E1555* *E1556* *E1557*
1947+
The number of concrete types provided when calling a generic function must
1948+
match the number of type variables in the function. An empty type list is not
1949+
allowed. Any Vim9 type (|vim9-types|) can be used as a concrete type in a
1950+
generic function.
1951+
1952+
Spaces are not allowed between the function name and "<", or between ">" and
1953+
the opening "(".
1954+
1955+
A generic function can be exported and imported like a regular function.
1956+
See |:export| and |:import|.
1957+
1958+
A generic function can be defined inside another regular or generic function.
1959+
1960+
Referencing type variables in generic types~
1961+
1962+
Instead of concrete types, type variables can be used with generic types.
1963+
This is useful for complex data structures like lists of dictionaries or
1964+
dictionaries of lists. Example: >
1965+
1966+
vim9script
1967+
1968+
def Flatten<T>(x: list<list<T>>): list<T>
1969+
var result: list<T> = []
1970+
for inner in x
1971+
result += inner
1972+
endfor
1973+
return result
1974+
enddef
1975+
1976+
echo Flatten<number>([[1, 2], [3]])
1977+
<
1978+
1979+
Generic class method~
1980+
1981+
A Vim9 class method can be a generic function: >
1982+
1983+
class A
1984+
def Foo<X, Y>()
1985+
enddef
1986+
endclass
1987+
var a = A.new()
1988+
a.Foo<number, string>()
1989+
<
1990+
*E1432* *E1433* *E1434*
1991+
A generic class method in a base class can be overridden by a generic method
1992+
in a child class. The number of type variables must match between both
1993+
methods. A concrete class method cannot be overridden by a generic method,
1994+
and vice versa.
1995+
1996+
Generic function reference~
1997+
1998+
A function reference (|Funcref|) can be a generic function. This allows for
1999+
creating factories of functions that operate on specific types: >
2000+
2001+
vim9script
2002+
2003+
def MakeEcho<T>(): func(T): T
2004+
return (x: T): T => x
2005+
enddef
2006+
2007+
var EchoNumber = MakeEcho<number>()
2008+
echo EchoNumber(123)
2009+
2010+
var EchoString = MakeEcho<string>()
2011+
echo EchoString('abc')
2012+
<
2013+
Compiling and Disassembling Generic functions~
2014+
2015+
The |:defcompile| command can be used to compile a generic function with a
2016+
specific list of concrete types: >
2017+
2018+
defcompile MyFunc<number, list<number>, dict<string>>
2019+
<
2020+
The |:disassemble| command can be used to list the instructions generated for
2021+
a generic function: >
2022+
2023+
disassemble MyFunc<string, dict<string>>
2024+
disassemble MyFunc<number, list<blob>>
2025+
<
2026+
Limitations and Future Work~
2027+
2028+
Currently, Vim does not support:
2029+
- Type inference for type variables: All types must be explicitly specified
2030+
when calling a generic function.
2031+
- Type constraints: It's not possible to restrict a type variable to a
2032+
specific class or interface (e.g., `T extends SomeInterface`).
2033+
- Default type arguments: Providing a default type for a type parameter
2034+
when not explicitly specified.
2035+
2036+
==============================================================================
2037+
2038+
6. Namespace, Import and Export
18992039
*vim9script* *vim9-export* *vim9-import*
19002040

19012041
A Vim9 script can be written to be imported. This means that some items are
@@ -2174,7 +2314,7 @@ Or: >
21742314
21752315
==============================================================================
21762316

2177-
6. Classes and interfaces *vim9-classes*
2317+
7. Classes and interfaces *vim9-classes*
21782318

21792319
In legacy script a Dictionary could be used as a kind-of object, by adding
21802320
members that are functions. However, this is quite inefficient and requires
@@ -2188,7 +2328,7 @@ functionality it is located in a separate help file: |vim9class.txt|.
21882328

21892329
==============================================================================
21902330

2191-
9. Rationale *vim9-rationale*
2331+
8. Rationale *vim9-rationale*
21922332

21932333
The :def command ~
21942334

0 commit comments

Comments
 (0)