labels: Anonymous records via named tuples

[ bsd3, development, library ] [ Propose Tags ]

Declare and access tuple fields with labels. An approach to anonymous records.


[Skip to Readme]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.0.0, 0.1.0, 0.1.1, 0.1.2, 0.2.0, 0.3.0, 0.3.1, 0.3.2, 0.3.3
Change log CHANGELOG
Dependencies base (>=4.7 && <5), template-haskell [details]
License BSD-3-Clause
Copyright 2016 Chris Done
Author Chris Done
Maintainer chrisdone@gmail.com
Category Development
Home page https://github.com/chrisdone/labels#readme
Source repo head: git clone https://github.com/chrisdone/labels
Uploaded by ChrisDone at 2017-01-20T11:21:14Z
Distributions LTSHaskell:0.3.3, NixOS:0.3.3, Stackage:0.3.3
Reverse Dependencies 3 direct, 9 indirect [details]
Downloads 6209 total (31 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2017-01-20 [all 1 reports]

Readme for labels-0.1.2

[back to package description]

labels

Declare and access tuple fields with labels

This package is experimental, exploring the design space opened up by the implemented and to-be-implemented work on extensible records in GHC.

Note: You need GHC 8.0.1 for the #foo syntax, otherwise you have to use $("foo") which works on GHC 7.10.

Basic examples

The haddock docs are here.

Enable these extensions:

  • In GHCi: :set -XOverloadedLabels -XTypeOperators -XDataKinds -XFlexibleContexts

  • In a module: {-# LANGUAGE OverloadedLabels, TypeOperators, DataKinds, FlexibleContexts #-}

Let's use GHCi:

> import Labels
> :set -XOverloadedLabels -XTypeOperators -XDataKinds -XFlexibleContexts
Construct a record
> (#foo := "hi", #bar := 123)
(#foo := "hi",#bar := 123)
Get fields of a record
> get #bar (#foo := "hi", #bar := 123)
123
Set fields of a record
> set #bar 66 (#foo := "hi", #bar := 123)
(#foo := "hi",#bar := 66)
Modify fields of a record
> modify #mu (*0.1) (#bar := "hi", #mu := 123)
(#bar := "hi",#mu := 12.3)
Add fields to a record
> cons (#mu := [1,2,3]) (#foo := "hi", #bar := 123)
(#mu := [1,2,3],#foo := "hi",#bar := 123)
Abstraction
> let double field record = set field (get field record * 2) record
> double #mu (#bar := "hi", #mu := 123)
(#bar := "hi",#mu := 246)