冗語構文 (F#)

F# 言語の多くの構成要素では、冗語構文と軽量構文の 2 つの形式の構文を使用できます。冗語構文は、軽量構文ほど一般的には使用されませんが、インデントへの依存が少ないという利点があります。軽量構文は、冗語構文よりも短く、begin、end、in などの追加のキーワードの代わりにインデントを使用して、構成要素の先頭と末尾を示します。既定の構文は、軽量構文です。このトピックでは、軽量構文が有効になっていない場合の F# の構成要素の構文について説明します。冗語構文は常に有効です。したがって、軽量構文を有効にした場合でも、一部の構成要素に冗語構文を使用できます。軽量構文を無効にするには、#light "off" ディレクティブを使用します。

構成要素の表

次の表に、F# の言語構成要素の軽量構文と冗語構文を、この 2 つの形式によって違いが生じるコンテキストで示します。この表で、山かっこ (<>) は、ユーザーが指定した構文要素を囲みます。これらの構成要素内で使用する構文の詳細については、各言語構成要素のドキュメントを参照してください。

言語構成要素

軽量構文

冗語構文

複合式

<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 .. 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

構造体

[<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

module

module <module-name> =
    ...
module <module-name> =
    begin
        ...
    end

参照

概念

コードのフォーマットに関するガイドライン (F#)

その他の技術情報

F# 言語リファレンス

コンパイラ ディレクティブ (F#)