HaskellのRecord
HaskellのRecord
code:hs
data User = User
{ userId :: Int
, name :: String
, age :: Int
}
let user = User { userId = 3, name = "tarou", age = 28 }
name user -- > "tarou"
// update
let changeName = user { name = "hanako" } -- > { userId = 3, name = "hanako", age = 28}
Record型を定義すると同時に色々なものが暗黙的に導入される
code:hs
data Foo = FooC { baz :: Int }
このような型を定義すると以下のものが導入される
① 型Foo
② Constructor Fooc
③ 構築の為のbaz
code:hs
foo1 = Foo { baz = 1 }
④ 更新の為のbaz
code:hs
update :: Foo -> Int -> Foo
update foo newId = foo { baz = newId }
これって、言語的には③と同じなのかなmrsekut.icon
⑤パターンマッチの為のbaz
code:hs
patt = \case
Foo { baz = 1 } -> ...
...
⑥ getterの為のbaz
code:hs
get :: Foo -> Int
get = baz
HaskellのRecordの問題点の解決策の遍歴
https://exploring-better-ways.bellroy.com/the-unreasonable-effectiveness-of-polymorphic-records.html
http://syocy.hatenablog.com/entry/2017/08/14/235830
http://xenophobia.hatenablog.com/entry/2015/03/14/184424
https://github.com/lotz84/haskell/blob/master/docs/extensible-record.md
https://matsubara0507.github.io/posts/2017-11-28-fun-of-extensible-1.html
https://ja.stackoverflow.com/questions/5278/haskell-の-レコード構文record-syntaxにて-簡潔なフィールド名を定義すると重複しやすい問題の解決方法
https://assets.adobe.com/public/b93f214d-58c2-482f-5528-a939d3e83660