F# の多くのコンストラクトで使用できる構文には、 詳細構文 と 軽量構文の 2 つの形式があります。 詳細構文は一般的には使用されませんが、インデントの影響を受けにくいという利点があります。 軽量構文は短く、インデントを使用して、 begin
、 end
、 in
などの追加のキーワードではなく、コンストラクトの開始と終了を通知します。 既定の構文は軽量構文です。 このトピックでは、軽量構文が有効になっていない場合の F# コンストラクトの構文について説明します。 詳細構文は常に有効になっているため、軽量構文を有効にした場合でも、一部のコンストラクトには詳細構文を使用できます。
コンストラクトの表
次の表は、2 つの形式に違いがあるコンテキストでの F# 言語コンストラクトの軽量で詳細な構文を示しています。 この表では、山かっこ (<>) でユーザーが指定した構文要素を囲みます。 これらのコンストラクト内で使用される構文の詳細については、各言語コンストラクトのドキュメントを参照してください。
言語コンストラクト |
軽量構文 |
詳細構文 |
複合式 |
<expression1>
<expression2>
|
<expression1>; <expression2>
|
入れ子になった let バインド
|
let f x =
let a = 1
let b = 2
x + a + b
|
let f x =
let a = 1 in
let b = 2 in
x + a + b
|
コード ブロック |
(
<expression1>
<expression2>
)
|
begin
<expression1>;
<expression2>;
end
|
'for...do' |
for counter = start to finish do
...
|
for counter = start to finish do
...
done
|
'while...do' |
while <condition> do
...
|
while <condition> do
...
done
|
'for...in' |
for var in start .. finish do
...
|
for var in start .. finish do
...
done
|
'do' |
do
...
|
do
...
in
|
記録 |
type <record-name> =
{
<field-declarations>
}
<value-or-member-definitions>
|
type <record-name> =
{
<field-declarations>
}
with
<value-or-member-definitions>
end
|
クラス |
type <class-name>(<params>) =
...
|
type <class-name>(<params>) =
class
...
end
|
構造体 |
[<StructAttribute>]
type <structure-name> =
...
|
type <structure-name> =
struct
...
end
|
判別共用体 |
type <union-name> =
| ...
| ...
...
<value-or-member definitions>
|
type <union-name> =
| ...
| ...
...
with
<value-or-member-definitions>
end
|
インターフェイス |
type <interface-name> =
...
|
type <interface-name> =
interface
...
end
|
オブジェクト式 |
{ new <type-name>
with
<value-or-member-definitions>
<interface-implementations>
}
|
{ new <type-name>
with
<value-or-member-definitions>
end
<interface-implementations>
}
|
インターフェイスの実装 |
interface <interface-name>
with
<value-or-member-definitions>
|
interface <interface-name>
with
<value-or-member-definitions>
end
|
type extension |
type <type-name>
with
<value-or-member-definitions>
|
type <type-name>
with
<value-or-member-definitions>
end
|
モジュール |
module <module-name> =
...
|
module <module-name> =
begin
...
end
|
こちらも参照ください