Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

クイックリファレンス

💡 お知らせ: このドキュメントはAIによって翻訳されています。表現に違和感がある場合は、原文(英語)を参照するか、翻訳にご協力ください。

Flix 言語の1ページチートシートです。各節は、詳細が書かれている対応する章へのリンクを含んでいます。

プログラムの構造

/// ドキュメントコメント。通常のコメントは // で始まります。
def main(): Unit \ IO =
    println("Hello, world!")

式は ; で区切ります。def は関数を、let はローカル変数を定義します (関数を参照):

def area(w: Int32, h: Int32): Int32 = w * h

def greeting(name: String): String \ IO =
    let msg = "Hello, ${name}!";
    println(msg);
    msg

無名関数には -> を使います:x -> x + 1。パイプラインには |> を使います: l |> List.length |> println。

プリミティブ型

Unit、Bool、Char、String、Int8、Int16、Int32、Int64、 BigInt、Float32、Float64、BigDecimal (プリミティブ型を参照)。

よく使われる演算子:+ - * / %、== != < <= > >=、and or not、 ::(リストのコンス)、:::(リストの連結)。

分岐

if (x < 0) {
    "negative"
} else if (x == 0) {
    "zero"
} else {
    "positive"
}

If-Then-Else を参照してください。

列挙型とパターンマッチング

enum Shape {
    case Circle(Int32)
    case Rectangle(Int32, Int32)
}

def area(s: Shape): Int32 = match s {
    case Shape.Circle(r)       => 3 * (r * r)
    case Shape.Rectangle(h, w) => h * w
}

列挙型はパラメトリックにできます:enum Tree[a] { case Leaf(a), case Node(Tree[a], Tree[a]) }。 ワイルドカードには case _ => ... を使います。タプルも同様に分解できます (列挙型とパターンマッチングを参照):

match pair {
    case (1, _) => "starts with one"
    case _      => "something else"
}

リスト

let l1 = 1 :: 2 :: 3 :: Nil;
let l2 = l1 ::: 4 :: Nil;
match l1 {
    case Nil     => 0
    case _ :: xs => 1 + List.length(xs)
}

リストを参照してください。

文字列

let name = "Ada";
let msg = "Hello, ${name}!";  // 文字列補間

文字列補間を参照してください。

レコード

let r = {name = "Ada", age = 36};
r#name                            // フィールドへのアクセス
{name = "Grace" | r}              // フィールドの更新
{+lang = "Flix" | r}              // フィールドの追加
{-age | r}                        // フィールドの削除

レコードを参照してください。

モジュール

use Calc.sum                      // 名前を use する
use Calc.{mul, neg}               // 複数の名前を use する
use Calc.{sum => plus}            // 別の名前で use する

mod Calc {
    pub def sum(x: Int32, y: Int32): Int32 = x + y
    pub def mul(x: Int32, y: Int32): Int32 = x * y
    pub def neg(x: Int32): Int32 = -x
}

use は、それが記述されているファイルまたはモジュールの宣言より前に書きます。モジュールの宣言は一度だけであり、pub mod でない限り、それが宣言されているモジュールに対してプライベートです。pub mod はそれ専用のファイルに置きます:pub mod Museum.Entrance は src/Museum/Entrance.flix に置きます。

モジュールとモジュールの宣言を参照してください。

トレイトとインスタンス

trait Show[t] {
    pub def show(x: t): String
}

instance Show[Int32] {
    pub def show(x: Int32): String = "${x}"
}

トレイトを参照してください。

エフェクト

純粋関数は空のエフェクト \ {} を持ちます。不純な関数は、使用するものを 宣言します。例えば \ IO です。エフェクト多相な関数は、変数を使ってエフェクトを 抽象化します(エフェクトシステムを参照):

def inc(x: Int32): Int32 \ {} = x + 1
def twice(f: Int32 -> Int32 \ ef, x: Int32): Int32 \ ef = f(f(x))

Option と Result

match opt {
    case Some(x) => x
    case None    => 0
}

match res {
    case Ok(v)  => v
    case Err(e) => 0
}

ループ

def main(): Unit \ IO =
    let l = 1 :: 2 :: 3 :: Nil;
    foreach (x <- l)
        println(x)

Foreach を参照してください。