Data.Intertwine.MkIso
- Package
- purescript-intertwine
- Repository
- collegevine/purescript-intertwine
#MkIso Source
class MkIso t ctor tuple | t ctor -> tuple where
This type class provides the function iso
, which operates on a sum type.
It takes the name of a constructor of that type and returns an Iso
instance that converts between the sum type itself and the chosen
constructor's parameters that are all tupled together, starting with the
right ones.
For example:
data T = A | B String | C Int Number | D Boolean Int String
iso (SProxy :: SProxy "A") :: Iso Unit T
iso (SProxy :: SProxy "B") :: Iso String T
iso (SProxy :: SProxy "C") :: Iso (Tuple Int Number) T
iso (SProxy :: SProxy "D") :: Iso (Tuple Boolean (Tuple Int String)) T
Such tupling is necessary for the implementation of both printers and
parsers from the same code structure. See
Syntax
for a more detailed explanation.
The resulting Iso
can always convert "forward" (i.e. from
tupled arguments to T
), but it can only convert "backward" (i.e. from T
to the corresponding tuple) when the given T
value was constructed with
the given constructor, returning Nothing
for all other constructors.
Members
Instances
(ArgsAsTuple args argsAsTuple) => MkIso (Constructor name args) name argsAsTuple
(ArgsAsTuple args argsAsTuple) => MkIso (Sum (Constructor ctor args) rest) ctor argsAsTuple
(MkIso rest ctor tuple) => MkIso (Sum (Constructor anotherName y) rest) ctor tuple
(Generic t rep, MkIso rep ctor tuple) => MkIso t ctor tuple
#ArgsAsTuple Source
class ArgsAsTuple args tuple | args -> tuple where
This type class takes the Generic-rep representation of sum type arguments and converts them into a series of nested tuples.
Members
argsToTuple :: args -> tuple
tupleToArgs :: tuple -> args
Instances
ArgsAsTuple NoArguments Unit
ArgsAsTuple (Argument a) a
(ArgsAsTuple a ax, ArgsAsTuple b bx) => ArgsAsTuple (Product a b) (Tuple ax bx)