type-machine-0.1.0.1: Type-level functions for record types
Safe HaskellNone
LanguageHaskell2010

TypeMachine.Functions

Synopsis

Fields

pick :: [String] -> Type -> TM Type Source #

Pick/Select fields by name

Issues a warning when a key is not in the input Type

 > data A = A { a :: Int, b :: Int }
 > pick ["a", "c"] <:> toType ''A

 data _ = { a :: Int }

omit :: [String] -> Type -> TM Type Source #

Remove fields by name

Issues a warning when a key is not in the input Type

 > data A = A { a :: Int, b :: Int }
 > omit ["b", "c"] <:> toType ''A

 data _ = { a :: Int }

Record

record :: [String] -> Q Type -> TM Type Source #

Creates a type from a list of keys and a ToFieldType

Issues a log if some keys are duplicated

 > record ["a", "b"] [t|Int|]

 data _ = { a :: Int, b :: Int }

Union and Intersection

intersection :: Type -> Type -> TM Type Source #

Keep the fields present in both types

If keys overlap, prefer the type of the left type

 > data A = A { a :: Int, b :: Int }
 > data B = B { b :: String, c :: Void }
 > intersection <:> toType ''A  : toType ''B

 data _ = { b :: Int }

intersection' :: Type -> Type -> TM Type Source #

Keep the fields present in both types

If keys overlap, prefer the type of the right type

 > data A = A { a :: Int, b :: Int }
 > data B = B { b :: String, c :: Void }
 > intersection' <:> toType ''A  : toType ''B

 data _ = { b :: String }

union :: Type -> Type -> TM Type Source #

Merge two types together

If keys overlap, prefer the type of the left type

 > data A = A { a :: Int, b :: Int }
 > data B = B { b :: String, c :: Void }
 > union <:> toType ''A  : toType ''B

 data _ = { a :: Int, b :: Int, c :: Void }

union' :: Type -> Type -> TM Type Source #

Merge two types together

If keys overlap, prefer the type of the right type

 > data A = A { a :: Int, b :: Int }
 > data B = B { b :: String, c :: Void }
 > union <:> toType ''A  : toType ''B

 data _ = { a :: Int, b :: String, c :: Void }

Optional

require :: [String] -> Type -> TM Type Source #

Mark fields are required

 > data A = A { a :: Maybe Int, b :: Int }
 > require ["a"] <:> toType ''A

 data _ = { a :: Int, b :: Int }

partial :: Type -> TM Type Source #

Marks all fields as Maybe

Fields that already have a Maybe type will not be changed.

 > data A = A { a :: Int, b :: Maybe Int }
 > partial <:> toType ''A

 data _ = { a :: Maybe Int, b :: Maybe Int }

partial' :: Type -> TM Type Source #

Similar to partial. Marks all fields as Maybe

Fields that already have a Maybe type will be wrapped in a Maybe

 > data A = A { a :: Int, b :: Maybe Int }
 > partial' <:> toType ''A

 data _ = { a :: Maybe Int, b :: Maybe (Maybe Int) }

Type Parameters

apply :: Q Type -> Type -> TM Type Source #

Replaces the first type parameter with the given type

Issues a warning if there are no type variable

 data A x = { a :: Maybe x }

 > apply [t|Int|] <:> toType ''A

 data _ = { a :: Maybe Int }

applyMany :: [Q Type] -> Type -> TM Type Source #

Applies multiple type arguments

Issues a warning if too many arguments are given

 data A a b = { a :: Maybe a, b :: b }

 > applyMany ([t|Int|], [t|String|]) <:> toType ''A

 data _ = { a :: Maybe Int, b :: String }

Utils

keysOf :: Type -> TM [String] Source #

Get the names of the fields in in type

 > data A = A { a :: Int, b :: Int }
 > keysOf <:> toType ''A

 ["a", "b"]