IsLabel型クラス
code:hs
class IsLabel (x :: Symbol) a where
fromLabel :: a
昔は個別のRecordごとにinstanceを定義する必要があった
code:hs
instance IsLabel "hoge" (Hoge -> Int) where
fromLabel Hoge { hoge } = hoge
今はこれ一つ定義しておけば良い
code:hs
{-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE ScopedTypeVariables #-} import GHC.OverloadedLabels (IsLabel (fromLabel))
import GHC.Records (HasField (getField))
instance HasField x r a => IsLabel x (r -> a) where
fromLabel = getField @x