F#の型
unit
()
直積型
code:fs
type aPerson = {First: string, Last: string} // 型定義
let aPerson = {First="Alex", Last="Adamas"} // 利用
アクセス
code:fs
let {First=first; Last=last} = aPerson
// もしくは
let first = aPerson.First
let last = aPerson.Last
直和型
code:fs
type OrderQuantity =
| UnitQuantity of int
| KilogramQuantity of decimal
// 利用
let anOrderQtyInUnits = UnitQuantity 10
let anOrderQtyIngKg = KilogramQuantity 2.5
Option
code:fs
type Option<'a> =
| Some of 'a
| None
直積型内でoptionを使う場合
普通に書いたらこう
code:fs
type a = {
id: int
name: Option<string>
}
こうも書ける
こっちのほうがよく書かれるっぽい
code:fs
type a = {
id: int
name: string option
}
Result
code:fs
type Result<'Success, 'Failure> =
| Ok of 'Success
| Error of 'Failure
コレクション型
いっぱいあるmrsekut.icon
list
リンクリスト
固定サイズでimmutable
optionのときと同じくrecordの中ではこう書ける
code:fs
type a = {
id: int
lines: OrderLine list
}
区切りはコンマではなくセミコロン
code:fs
パターンマッチングもHaskellと同じ
[x;y]なら2要素リスト、first::restなら最初と残り
array
固定サイズでmutable
indexでアクセス可
ResizeArray
可変サイズのarray
アイテムの追加、削除ができる
C#のList<T>と同じ
seq
遅延評価されるリスト
個々の要素は必要になったら評価される
必ずしも全ての要素を使用するとは限らないときに便利
C#のIEnumerable<T>
Map
Set
exn型
例外を表す型
Async型
非同期処理
TSのPromise型みたいな?