summaryrefslogtreecommitdiff
path: root/README.md
blob: 89c90c0f537e6f87f5392e6ec0ff03e0aaf057d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# Bookkeeper [![Build Status][ci-img]][ci]

Bookkeeper is a new Haskell library that uses the new OverloadedLabels feature
of GHC 8 to provide a new take on datatypes and records:

~~~ {.haskell}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE OverloadedLabels #-}
import Bookkeeper


jane :: Book '[ "name" :=> String, "age" :=> Int ]
jane = emptyBook
     & #name =: "Jane"
     & #age =: 30

-- >>> jane
-- Book {age = 30, name = "Jane"}
-- >>> jane ?: #name
-- "Jane"
~~~

It bears some similarities to Nikita Volkov's [record](https://nikita-volkov.github.io/record/)
library, but requires no Template Haskell.

Accesing a field that does not exist is a type error, made nicer with GHCs new
custom type errors:

~~~ {.haskell}
 -- >>> jane ?: #address
--   • The provided Book does not contain the field "address"
--     Book type:
--     '["age" ':-> Int, "name" ':-> String]
~~~


The order in which fields are inserted or appear in types does not matter. That
is, in:

~~~ {.haskell}
-- type A = Book '[ "field1" :=> Int,  "field2" :=> Bool]
-- type B = Book '[ "field2" :=> Bool, "field1" :=> Int ]
~~~

Types `A` and `B` are the same.

You can set, modify, or get fields. See the haddocks for more information.


~~~ {.haskell}
main :: IO ()
main = return ()
~~~

[ci-img]:   https://travis-ci.org/turingjump/bookkeeper.svg?branch=master
[ci]:       https://travis-ci.org/turingjump/bookkeeper