1
1
mirror of https://github.com/sdiehl/wiwinwlh.git synced 2024-09-11 12:05:25 +03:00

Include code snippet

This commit is contained in:
David Obwaller 2021-08-22 13:08:32 +02:00
parent c45d827783
commit 1f6adff2a3

View File

@ -5898,29 +5898,8 @@ extract :: IsLabel "id" t => t
extract = #id
```
```haskell
{-# LANGUAGE OverloadedLabels #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE ExistentialQuantification #-}
import GHC.Records (HasField(..)) -- Since base 4.10.0.0
import GHC.OverloadedLabels (IsLabel(..))
data S = MkS { foo :: Int }
data T x y z = forall b . MkT { foo :: y, bar :: b }
instance HasField x r a => IsLabel x (r -> a) where
fromLabel = getField @x
main :: IO ()
main = do
print (#foo (MkS 42))
print (#foo (MkT True False))
```
~~~~ {.haskell include="src/03-monad-transformers/overloaded_labels.hs"}
~~~~
This is used in more advanced libraries like [Selda] which do object relational
mapping between Haskell datatype fields and database columns.