named-records: Flexible records with named fields.
Flexible records with named fields.
v0.2
- Default values with
record
. v0.2.1
- Requires
names-0.2.1
. v0.2.2
- TH
record
definitions allow for more types to be used in the definition.
Named records allow you to define und use records with labeled fields. These records are first class objects. Record fields are labeled by names, which can basically be any type. However, the names package provides global name types and some syntactic sugar to use them.
Here is a complete walk-through, with Template Haskell syntactic sugar.
This is how a typical example preamble looks like:
{-# LANGUAGE Haskell2010, TemplateHaskell #-} import qualified Data.Name import Data.NamedRecord
In order to use names you need to declare them first
(see the names
package for further details):
name "firstName" name "lastName"
These are two records Person
and User
:
record "Person" `has` "firstName" := ''String `has` "lastName" := ''String record "User" `has` "firstName" := ''String `has` "lastName" := ''String `has` "loginName" := ''String
Note that these declarations create constructor
functions newPerson
and newUser
, as well as
type synonyms Person
and User
(use -ddump-slices
to see what has been generated).
Here are two instances of these recors:
julian = newPerson `set` firstName := "Julian" `set` lastName := "Fleischer" alexander = newUser `set` firstName := "Alexander" `set` lastName := "Carnicero" `set` loginName := "alexander.carnicero"
We can now create a displayName
function like
the following:
displayName obj = (obj `get` firstName) ++ " " ++ (obj `get` lastName)
Note that this function will accept any record
that has a firstName
and a lastName
field of
type String
.
>>>
displayName julian
Julian Fleischer
>>>
displayName alexander
Alexander Carnicero
As mentioned above, records are first class citizens. That means you can create them anywhere:
>>>
displayName (firstName := "John" :+ lastName := "Doe")
John Doe
Downloads
- named-records-0.2.2.tar.gz [browse] (Cabal source package)
- Package description (as included in the package)
Maintainer's Corner
For package maintainers and hackage trustees
Candidates
- No Candidates
Versions [RSS] | 0.1, 0.2, 0.2.1, 0.2.2, 0.2.3, 0.3, 0.3.1, 0.3.2, 0.3.3, 0.4, 0.5 |
---|---|
Dependencies | base (>=4 && <5), names (==0.2.2), template-haskell (>=2.7) [details] |
License | MIT |
Author | Julian Fleischer <julian.fleischer@fu-berlin.de> |
Maintainer | Julian Fleischer <julian.fleischer@fu-berlin.de> |
Category | Data, Records |
Uploaded | by JulianFleischer at 2013-03-30T14:02:18Z |
Distributions | NixOS:0.5 |
Reverse Dependencies | 1 direct, 0 indirect [details] |
Downloads | 8234 total (6 in the last 30 days) |
Rating | (no votes yet) [estimated by Bayesian average] |
Your Rating | |
Status | Docs uploaded by user [build log] Successful builds reported [all 1 reports] |