冗語構文

F# には、さまざまなコンストラクトに使用できる "冗語構文" と "軽量構文" の 2 つの形式の構文があります。 冗語構文はあまり使用されませんが、インデントの影響が少ないという利点があります。 軽量構文は短く、beginendin のような追加キーワードではなく、インデントを使用し、コンストラクトの冒頭と末尾を通知します。 既定の構文は軽量構文です。 このトピックでは、軽量構文が有効になっていないときの、F# コンストラクト用の構文について説明します。 冗語構文は常に有効になります。軽量構文を有効にする場合でも、一部のコンストラクトに冗語構文を使用できます。

コンストラクトの表

次の表には、軽量構文と冗語構文に違いがあるコンテキストにおける、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
class
type <class-name>(<params>) =
    ...
type <class-name>(<params>) =
    class
        ...
    end
structure
[<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
interface
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 <type-name>
    with
        <value-or-member-definitions>
type <type-name>
    with
        <value-or-member-definitions>
    end
name
module <module-name> =
    ...
module <module-name> =
    begin
        ...
    end

関連項目