generated at
NamedFieldPuns
recordを引数に取る時に、fieldのパターンマッチを略記する


何が変わるのか
通常は、Recordを引数に取る時にパターンマッチができる
hs
data Hoge = Hoge { h1 :: Int, h2 :: Int } f :: Hoge -> Int f Hoge { h1 = 2 } = 2 -- h1が`2`ならマッチ
同じノリでパターンマッチによって値を取り出せる
hs
f :: Hoge -> Int f Hoge { h1 = n } = n
実際は、名前を揃えてこう書かれることが多い
hs
f Hoge { h1 = h1 } = h1
これを略記するのが NamedFieldPuns
こう書ける
hs
f :: Hoge -> Int f Hoge { h1 } = h1
複数もok
hs
f Hoge { h1, h2 } = h1 + h2
hs
f h@Hoge { h1, h2 } = ...
Consturcot時も使える
hs
mkHoge h1 h2 = Hoge { h1, h2 } -- mkHoge h1 h2 = Hoge { h1 = h1, h2 = h2} 通常はこう書く



JSの記法に似ている
js
const f = ({id: x}) => x const g = ({id}) => id



さらに略記させるものにRecordWildCardsがある