Skip to content

Commit 402f851

Browse files
authored
Merge pull request #1419 from vim-jp/hh-update-vim9
Update vim9.{txt,jax}
2 parents b6d25e9 + 3b35852 commit 402f851

File tree

2 files changed

+325
-14
lines changed

2 files changed

+325
-14
lines changed

doc/vim9.jax

Lines changed: 164 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*vim9.txt* For Vim バージョン 9.1. Last change: 2023 Dec 09
1+
*vim9.txt* For Vim バージョン 9.1. Last change: 2024 Jan 12
22

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

@@ -1063,8 +1063,11 @@ Vim9 script では以下の定義済みの値が使えます: >
10631063
10641064
また、それらの値は引数の既定値とするのにも便利です: >
10651065
def MyFunc(b: blob = null_blob)
1066-
if b == null_blob
1067-
# 引数 b が与えられなかった
1066+
# Note: null_blob ではなく null と比較し、
1067+
# デフォルト値と空 blob を区別する。
1068+
if b == null
1069+
# 引数 b が与えられなかった
1070+
null に対するテストについての詳細は、|null-compare| を参照。
10681071

10691072
`null` はどんな値と比較すること可能で、型エラーが発生することはありません。
10701073
しかし、`null` と数値、浮動小数点数、真偽値との比較は常に `false` になります。
@@ -1267,10 +1270,13 @@ Note 認識されていないコマンドを "|" でつなぐと、その後の
12671270
なりません。それらは関数内で、旧来の関数内でも作成することはできません。
12681271

12691272
*:defc* *:defcompile*
1270-
:defc[ompile] 現在のスクリプトで定義されている関数のうち、まだコンパ
1271-
イルされていないものをコンパイルします。これはコンパイ
1272-
ル時に見つかったいかなるエラーも報告します。
1273-
クラス内部で定義された関数は除きます。
1273+
:defc[ompile] 現在のスクリプトで定義されている関数とクラス
1274+
(|class-compile|) のうち、まだコンパイルされていないも
1275+
のをコンパイルします。これはコンパイル時に見つかったい
1276+
かなるエラーも報告します。
1277+
1278+
:defc[ompile] MyClass クラス内のすべてのメソッドをコンパイルします。
1279+
|class-compile|
12741280

12751281
:defc[ompile] {func}
12761282
:defc[ompile] debug {func}
@@ -1697,6 +1703,157 @@ Vim9 script ではこれは厳格にされています。使われている値
16971703
*E1297* *E1298* *E1301*
16981704
間違いを発見しやすくするために、ほとんどの組み込み関数で型がチェックされます。
16991705

1706+
変数のカテゴリー、デフォルトおよび null の扱い ~
1707+
*variable-categories* *null-variables*
1708+
変数には次のカテゴリがあります:
1709+
プリミティブ number, float, boolean
1710+
コンテナ string, blob, list, dict
1711+
特殊 function, job, channel, user-defined-object
1712+
1713+
初期化子を使用せずに変数を宣言する場合は、明示的に型を指定する必要があります。
1714+
各カテゴリには、異なるデフォルトの初期化セマンティクスがあります。カテゴリごと
1715+
の例を次に示します: >
1716+
var num: number # プリミティブのデフォルトは 0 相当
1717+
var cont: list<string> # コンテナのデフォルトは空コンテナ
1718+
var spec: job # 特殊変数のデフォルトは null
1719+
<
1720+
Vim にはおなじみの null 値はありません。|null_string||null_list||null_job|
1721+
など、さまざまな null_<type> があらかじめ定義されています。プリミティブは
1722+
null_<type> を持ちません。null_<type> の典型的な使用例は以下の通りです:
1723+
- 変数をクリアし、そのリソースを解放。
1724+
- 関数定義内のパラメータのデフォルトとしては、|null-compare| を参照。
1725+
1726+
`job` などの特殊な変数の場合、リソースをクリアするために null_<type> が使用さ
1727+
れます。コンテナ変数の場合、空コンテナを変数に割り当てることによってリソースを
1728+
クリアすることもできます。例: >
1729+
var j: job = job_start(...)
1730+
# ... ジョブはその仕事を行う
1731+
j = null_job # 変数をクリアしてジョブのリソースを解放する
1732+
1733+
var l: list<any>
1734+
# ... リストにたくさんのものを追加
1735+
l = [] # 変数をクリアしてコンテナリソースを解放する
1736+
null_<type> ではなく空コンテナを使用してコンテナ変数をクリアすると、
1737+
|null-anomalies| で説明されているように null の複雑な問題を回避できる可能性が
1738+
あります。
1739+
1740+
コンテナ変数と特殊な変数の初期化セマンティクスは異なります。初期化されていない
1741+
コンテナはデフォルトで空コンテナになります: >
1742+
var l1: list<string> # 空コンテナ
1743+
var l2: list<string> = [] # 空コンテナ
1744+
var l3: list<string> = null_list # null コンテナ
1745+
"l1" と "l2" は同等で区別できない初期化です。ただし、"l3" は null コンテナで
1746+
す。null コンテナは空コンテナに似ていますが、異なります。|null-anomalies| を参
1747+
照してください。
1748+
1749+
特殊変数のデフォルトは null です。これらのジョブの初期化は同等であり、区別でき
1750+
ません: >
1751+
var j1: job
1752+
var j2: job = null_job
1753+
var j3 = null_job
1754+
1755+
リストまたは辞書が宣言されている時に、項目の型が指定されておらず推論できない場
1756+
合、型は "any" になります: >
1757+
var d1 = {} # 型は "dict<any>"
1758+
var d2 = null_dict # 型は "dict<any>"
1759+
1760+
関数の宣言は特に独特です。|vim9-func-declaration| を参照。
1761+
1762+
*null-compare*
1763+
一般的な null 比較セマンティクスでは、null コンテナが空コンテナと等しくない場
1764+
合、比較で null_<type> を使用しないでください: >
1765+
vim9script
1766+
def F(arg: list<string> = null_list)
1767+
if arg == null
1768+
echo "null"
1769+
else
1770+
echo printf("not null, %sempty", empty(arg) ? '' : 'not ')
1771+
endif
1772+
enddef
1773+
F() # 出力: "null"
1774+
F(null_list) # 出力: "null"
1775+
F([]) # 出力: "not null, empty"
1776+
F(['']) # 出力: "not null, not empty"
1777+
上記の関数は文字列のリストを取得し、それについてレポートします。
1778+
さまざまな種類の引数を受け入れるように、上記の関数シグネチャを変更します: >
1779+
def F(arg: list<any> = null_list) # あらゆるタイプのリスト
1780+
def F(arg: any = null) # あらゆるタイプ
1781+
<
1782+
上記の例では、null リストと空リストを区別することが目的であり、`null_list`
1783+
はなく `null` と比較することが正しい選択です。基本的な理由は、
1784+
"null_list == null" および "[] != null" であるためです。
1785+
"[] == null_list" であるため、`null_list` との比較は失敗します。次のセクション
1786+
で比較結果の詳細が記載されています。
1787+
1788+
*null-details* *null-anomalies*
1789+
このセクションでは、null および null_<type> の使用に関する問題について説明しま
1790+
す。以下に、null 比較の列挙結果を示します。場合によっては、vim9 の null セマン
1791+
ティクスに精通している場合、プログラマは比較やその他の状況で null_<type> を使
1792+
用することを選択することがあります。
1793+
1794+
ドキュメントの他の場所には次のように書かれています:
1795+
多くの場合、null 値は空の値と同じように処理されますが、常にそうとは限
1796+
りません
1797+
以下に例を示します: >
1798+
vim9script
1799+
var s1: list<string>
1800+
var s2: list<string> = null_list
1801+
echo s1 # 出力: "[]"
1802+
echo s2 # 出力: "[]"
1803+
1804+
echo s1 + ['a'] # 出力: "['a']"
1805+
echo s2 + ['a'] # 出力: "['a']"
1806+
1807+
echo s1->add('a') # 出力: "['a']"
1808+
echo s2->add('a') # E1130: Can not add to null list
1809+
<
1810+
null_<type> に等しい 2 つの値は、必ずしも互いに等しいとは限りません: >
1811+
vim9script
1812+
echo {} == null_dict # true
1813+
echo null_dict == null # true
1814+
echo {} == null # false
1815+
<
1816+
他のコンテナとは異なり、初期化されていない文字列は null と等しくなります。`is`
1817+
演算子を使用して、null_string かどうかを判断できます: >
1818+
vim9script
1819+
var s1: string
1820+
var s2 = null_string
1821+
echo s1 == null # true - これは想定外
1822+
echo s2 == null # true
1823+
echo s2 is null_string # true
1824+
1825+
var b1: blob
1826+
var b2 = null_blob
1827+
echo b1 == null # false
1828+
echo b2 == null # true
1829+
<
1830+
null_<type> に初期化された変数はすべて、null_<type> と等しく、また null と等し
1831+
くなります。例: >
1832+
vim9script
1833+
var x = null_blob
1834+
echo x == null_blob # true
1835+
echo x == null # true
1836+
<
1837+
初期化されていない変数は通常、null と等しくなります。それはそのタイプによって
1838+
異なります:
1839+
var s: string s == null
1840+
var b: blob b != null ***
1841+
var l: list<any> l != null ***
1842+
var d: dict<any> d != null ***
1843+
var f: func f == null
1844+
var j: job j == null
1845+
var c: channel c == null
1846+
var o: Class o == null
1847+
1848+
空に初期化された変数は null_<type> と同等です。null ではありません:
1849+
var s2: string = "" == null_string != null
1850+
var b2: blob = 0z == null_blob != null
1851+
var l2: list<any> = [] == null_list != null
1852+
var d2: dict<any> = {} == null_dict != null
1853+
1854+
NOTE: ジョブなどの特殊な変数はデフォルトで null 値になり、対応する空の値はあり
1855+
ません。
1856+
17001857
==============================================================================
17011858

17021859
5. 名前空間、Import と Export

en/vim9.txt

Lines changed: 161 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: 2023 Dec 09
1+
*vim9.txt* For Vim version 9.1. Last change: 2024 Jan 12
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -1055,8 +1055,11 @@ variable, since they cannot be deleted with `:unlet`. E.g.: >
10551055
10561056
The values can also be useful as the default value for an argument: >
10571057
def MyFunc(b: blob = null_blob)
1058-
if b == null_blob
1059-
# b argument was not given
1058+
# Note: compare against null, not null_blob,
1059+
# to distinguish the default value from an empty blob.
1060+
if b == null
1061+
# b argument was not given
1062+
See |null-compare| for more information about testing against null.
10601063

10611064
It is possible to compare `null` with any value, this will not give a type
10621065
error. However, comparing `null` with a number, float or bool will always
@@ -1257,10 +1260,12 @@ Script-local variables in a |Vim9| script must be declared at the script
12571260
level. They cannot be created in a function, also not in a legacy function.
12581261

12591262
*:defc* *:defcompile*
1260-
:defc[ompile] Compile functions defined in the current script that
1261-
were not compiled yet.
1262-
This will report any errors found during compilation.
1263-
This excludes functions defined inside a class.
1263+
:defc[ompile] Compile functions and classes (|class-compile|)
1264+
defined in the current script that were not compiled
1265+
yet. This will report any errors found during
1266+
compilation.
1267+
1268+
:defc[ompile] MyClass Compile all methods in a class |class-compile|.
12641269

12651270
:defc[ompile] {func}
12661271
:defc[ompile] debug {func}
@@ -1698,6 +1703,155 @@ argument type checking: >
16981703
Types are checked for most builtin functions to make it easier to spot
16991704
mistakes.
17001705

1706+
Categories of variables, defaults and null handling ~
1707+
*variable-categories* *null-variables*
1708+
There are categories of variables:
1709+
primitive number, float, boolean
1710+
container string, blob, list, dict
1711+
specialized function, job, channel, user-defined-object
1712+
1713+
When declaring a variable without an initializer, an explicit type must be
1714+
provided. Each category has different default initialization semantics. Here's
1715+
an example for each category: >
1716+
var num: number # primitives default to a 0 equivalent
1717+
var cont: list<string> # containers default to an empty container
1718+
var spec: job # specialized variables default to null
1719+
<
1720+
Vim does not have a familiar null value; it has various null_<type> predefined
1721+
values, for example |null_string|, |null_list|, |null_job|. Primitives do not
1722+
have a null_<type>. The typical use cases for null_<type> are:
1723+
- to `clear a variable` and release its resources;
1724+
- as a `default for a parameter` in a function definition, see |null-compare|.
1725+
1726+
For a specialized variable, like `job`, null_<type> is used to clear the
1727+
resources. For a container variable, resources can also be cleared by
1728+
assigning an empty container to the variable. For example: >
1729+
var j: job = job_start(...)
1730+
# ... job does its work
1731+
j = null_job # clear the variable and release the job's resources
1732+
1733+
var l: list<any>
1734+
# ... add lots of stuff to list
1735+
l = [] # clear the variable and release container resources
1736+
Using the empty container, rather than null_<type>, to clear a container
1737+
variable may avoid null complications as described in |null-anomalies|.
1738+
1739+
The initialization semantics of container variables and specialized variables
1740+
differ. An uninitialized container defaults to an empty container: >
1741+
var l1: list<string> # empty container
1742+
var l2: list<string> = [] # empty container
1743+
var l3: list<string> = null_list # null container
1744+
"l1" and "l2" are equivalent and indistinguishable initializations; but "l3"
1745+
is a null container. A null container is similar to, but different from, an
1746+
empty container, see |null-anomalies|.
1747+
1748+
Specialized variables default to null. These job initializations are
1749+
equivalent and indistinguishable: >
1750+
var j1: job
1751+
var j2: job = null_job
1752+
var j3 = null_job
1753+
1754+
When a list or dict is declared, if the item type is not specified and can not
1755+
be inferred, then the type is "any": >
1756+
var d1 = {} # type is "dict<any>"
1757+
var d2 = null_dict # type is "dict<any>"
1758+
1759+
Declaring a function, see |vim9-func-declaration|, is particularly unique.
1760+
1761+
*null-compare*
1762+
For familiar null compare semantics, where a null container is not equal to
1763+
an empty container, do not use null_<type> in a comparison: >
1764+
vim9script
1765+
def F(arg: list<string> = null_list)
1766+
if arg == null
1767+
echo "null"
1768+
else
1769+
echo printf("not null, %sempty", empty(arg) ? '' : 'not ')
1770+
endif
1771+
enddef
1772+
F() # output: "null"
1773+
F(null_list) # output: "null"
1774+
F([]) # output: "not null, empty"
1775+
F(['']) # output: "not null, not empty"
1776+
The above function takes a `list of strings` and reports on it.
1777+
Change the above function signature to accept different types of arguments: >
1778+
def F(arg: list<any> = null_list) # any type of list
1779+
def F(arg: any = null) # any type
1780+
<
1781+
In the above example, where the goal is to distinguish a null list from an
1782+
empty list, comparing against `null` instead of `null_list` is the correct
1783+
choice. The basic reason is because "null_list == null" and "[] != null".
1784+
Comparing to `null_list` fails since "[] == null_list". In the following section
1785+
there are details about comparison results.
1786+
1787+
*null-details* *null-anomalies*
1788+
This section describes issues about using null and null_<type>; included below
1789+
are the enumerated results of null comparisons. In some cases, if familiar
1790+
with vim9 null semantics, the programmer may chose to use null_<type> in
1791+
comparisons and/or other situations.
1792+
1793+
Elsewhere in the documentation it says:
1794+
Quite often a null value is handled the same as an
1795+
empty value, but not always
1796+
Here's an example: >
1797+
vim9script
1798+
var s1: list<string>
1799+
var s2: list<string> = null_list
1800+
echo s1 # output: "[]"
1801+
echo s2 # output: "[]"
1802+
1803+
echo s1 + ['a'] # output: "['a']"
1804+
echo s2 + ['a'] # output: "['a']"
1805+
1806+
echo s1->add('a') # output: "['a']"
1807+
echo s2->add('a') # E1130: Can not add to null list
1808+
<
1809+
Two values equal to a null_<type> are not necessarily equal to each other: >
1810+
vim9script
1811+
echo {} == null_dict # true
1812+
echo null_dict == null # true
1813+
echo {} == null # false
1814+
<
1815+
Unlike the other containers, an uninitialized string is equal to null. The
1816+
'is' operator can be used to determine if it is a null_string: >
1817+
vim9script
1818+
var s1: string
1819+
var s2 = null_string
1820+
echo s1 == null # true - this is unexpected
1821+
echo s2 == null # true
1822+
echo s2 is null_string # true
1823+
1824+
var b1: blob
1825+
var b2 = null_blob
1826+
echo b1 == null # false
1827+
echo b2 == null # true
1828+
<
1829+
Any variable initialized to the null_<type> is equal to the null_<type> and is
1830+
also equal to null. For example: >
1831+
vim9script
1832+
var x = null_blob
1833+
echo x == null_blob # true
1834+
echo x == null # true
1835+
<
1836+
An uninitialized variable is usually equal to null; it depends on its type:
1837+
var s: string s == null
1838+
var b: blob b != null ***
1839+
var l: list<any> l != null ***
1840+
var d: dict<any> d != null ***
1841+
var f: func f == null
1842+
var j: job j == null
1843+
var c: channel c == null
1844+
var o: Class o == null
1845+
1846+
A variable initialized to empty equals null_<type>; but not null:
1847+
var s2: string = "" == null_string != null
1848+
var b2: blob = 0z == null_blob != null
1849+
var l2: list<any> = [] == null_list != null
1850+
var d2: dict<any> = {} == null_dict != null
1851+
1852+
NOTE: the specialized variables, like job, default to null value and have no
1853+
corresponding empty value.
1854+
17011855
==============================================================================
17021856

17031857
5. Namespace, Import and Export

0 commit comments

Comments
 (0)