Data.Functor.Variant
- Package
- purescript-variant
- Repository
- natefaubion/purescript-variant
#case_ Source
case_ :: forall b a. VariantF () a -> b
Combinator for exhaustive pattern matching.
caseFn :: VariantF (foo :: FProxy Maybe, bar :: FProxy (Tuple String), baz :: FProxy (Either String)) Int -> String
caseFn = case_
# on (SProxy :: SProxy "foo") (\foo -> "Foo: " <> maybe "nothing" show foo)
# on (SProxy :: SProxy "bar") (\bar -> "Bar: " <> show (snd bar))
# on (SProxy :: SProxy "baz") (\baz -> "Baz: " <> either id show baz)
#default Source
default :: forall r b a. a -> VariantF r b -> a
Combinator for partial matching with a default value in case of failure.
caseFn :: forall r. VariantF (foo :: FProxy Maybe, bar :: FProxy (Tuple String) | r) Int -> String
caseFn = default "No match"
# on (SProxy :: SProxy "foo") (\foo -> "Foo: " <> maybe "nothing" show foo)
# on (SProxy :: SProxy "bar") (\bar -> "Bar: " <> show (snd bar))
#match Source
match :: forall result typearg record variant. VariantFRecordMatching variant record typearg result => Record record -> VariantF variant typearg -> result
Match a variant
with a record
containing methods to handle each case
to produce a result
.
This means that if variant
contains a row of type FProxy f
, a row with
the same label must have type f a -> result
in record
, where result
is the same type for every row of record
.
Polymorphic methods in record
may create problems with the type system
if the polymorphism is not fully generalized to the whole record type
or if not all polymorphic variables are specified in usage. When in doubt,
label methods with specific types, such as show :: Int -> String
, or
give the whole record an appropriate type.
#contract Source
contract :: forall a f gt lt. Alternative f => Contractable gt lt => VariantF gt a -> f (VariantF lt a)
A VariantF gt a
can be cast to some VariantF lt a
, where lt
is is a subset
of gt
, as long as there is proof that the VariantF
's runtime tag is
within the subset of lt
.
Re-exports from Data.Symbol
Re-exports from Data.Variant.Internal
#FProxy Source
data FProxy (a :: Type -> Type)
Proxy for a Functor
.
Constructors
Instances
(VariantFMatchEachCase v r a res) => VariantFMatchEachCase (Cons sym (FProxy f) v) (Cons sym (f a -> res) r) a res
#Contractable Source
class Contractable gt lt
Instances
(RowToList lt ltl, Union lt a gt, VariantTags ltl) => Contractable gt lt