HaskellのRecord
HaskellのRecord
hsdata 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型を定義すると同時に色々なものが暗黙的に導入される
hsdata Foo = FooC { baz :: Int }
このような型を定義すると以下のものが導入される
① 型 Foo
② Constructor Fooc
③ 構築の為の baz
④ 更新の為の baz
hsupdate :: Foo -> Int -> Foo
update foo newId = foo { baz = newId }
これって、言語的には③と同じなのかな

⑤パターンマッチの為の baz
hspatt = \case
Foo { baz = 1 } -> ...
...
⑥ getterの為の baz
hsget :: Foo -> Int
get = baz