haspara-0.0.0.0: A library providing definitions to work with monetary values.
Safe HaskellNone
LanguageHaskell2010

Haspara.Id

Synopsis

Documentation

newtype Id a b Source #

Type encoding for entity identifiers.

This encoding allows us to provide a phantom type for distinguishing between identifiers of varying types and an underlying identifier type.

For example:

>>> data A = A
>>> data B = B
>>> data C = C
>>> type IdA = Id A Int
>>> type IdB = Id B Int
>>> type IdC = Id C String
>>> let idA = Id 1 :: IdA
>>> let idB = Id 1 :: IdB
>>> let idC = Id "C1" :: IdC
>>> idA
1
>>> idB
1
>>> idC
"C1"
>>> idA == idA
True
>>> -- idA == idB  -- Compile error as: Couldn't match type ‘B’ with ‘A’

Hashes, on the otherhand, can be compared:

>>> import Data.Hashable
>>> hash idA == hash idB
True

Constructors

Id 

Fields

Instances

Instances details
Eq b => Eq (Id a b) Source # 
Instance details

Defined in Haspara.Internal.Id

Methods

(==) :: Id a b -> Id a b -> Bool #

(/=) :: Id a b -> Id a b -> Bool #

Ord b => Ord (Id a b) Source # 
Instance details

Defined in Haspara.Internal.Id

Methods

compare :: Id a b -> Id a b -> Ordering #

(<) :: Id a b -> Id a b -> Bool #

(<=) :: Id a b -> Id a b -> Bool #

(>) :: Id a b -> Id a b -> Bool #

(>=) :: Id a b -> Id a b -> Bool #

max :: Id a b -> Id a b -> Id a b #

min :: Id a b -> Id a b -> Id a b #

Show b => Show (Id a b) Source # 
Instance details

Defined in Haspara.Internal.Id

Methods

showsPrec :: Int -> Id a b -> ShowS #

show :: Id a b -> String #

showList :: [Id a b] -> ShowS #

Hashable b => Hashable (Id a b) Source # 
Instance details

Defined in Haspara.Internal.Id

Methods

hashWithSalt :: Int -> Id a b -> Int #

hash :: Id a b -> Int #

ToJSON b => ToJSON (Id a b) Source # 
Instance details

Defined in Haspara.Internal.Id

Methods

toJSON :: Id a b -> Value #

toEncoding :: Id a b -> Encoding #

toJSONList :: [Id a b] -> Value #

toEncodingList :: [Id a b] -> Encoding #

FromJSON b => FromJSON (Id a b) Source # 
Instance details

Defined in Haspara.Internal.Id

Methods

parseJSON :: Value -> Parser (Id a b) #

parseJSONList :: Value -> Parser [Id a b] #

type IdLookup a b = HashMap (Id a b) a Source #

Type encoding for a lookup table from entity Ids to corresponding entities.

>>> data A = A Int String deriving Show
>>> type IdA = Id A Int
>>> let a1 = A 1 "a1"
>>> let a2 = A 2 "a2"
>>> let a3 = A 3 "a3"
>>> let table = HM.fromList [(Id 1, a1), (Id 2, a2), (Id 3, a3)] :: IdLookup A Int
>>> HM.lookup (Id 1) table
Just (A 1 "a1")