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:
parent
c45d827783
commit
1f6adff2a3
25
tutorial.md
25
tutorial.md
@ -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.
|
||||
|
Loading…
Reference in New Issue
Block a user