Data.Variant
- Package
- purescript-variant
- Repository
- natefaubion/purescript-variant
#Variant Source
data Variant :: Row Type -> Type
Instances
(RowToList r rl, VariantTags rl, VariantEqs rl) => Eq (Variant r)
(RowToList r rl, VariantTags rl, VariantEqs rl, VariantOrds rl) => Ord (Variant r)
(RowToList r rl, VariantTags rl, VariantShows rl) => Show (Variant r)
#onMatch Source
onMatch :: forall b r3 r2 r1 r rl. RowToList r rl => VariantMatchCases rl r1 b => Union r1 r2 r3 => Record r -> (Variant r2 -> b) -> Variant r3 -> b
Match a Variant
with a Record
containing functions for handling cases.
This is similar to on
, except instead of providing a single label and
handler, you can provide a record where each field maps to a particular
Variant
case.
onMatch
{ foo: \foo -> "Foo: " <> foo
, bar: \bar -> "Bar: " <> bar
}
Polymorphic functions in records (such as show
or id
) can lead
to inference issues 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.
#case_ Source
case_ :: forall a. Variant () -> a
Combinator for exhaustive pattern matching.
caseFn :: Variant (foo :: Int, bar :: String, baz :: Boolean) -> String
caseFn = case_
# on (SProxy :: SProxy "foo") (\foo -> "Foo: " <> show foo)
# on (SProxy :: SProxy "bar") (\bar -> "Bar: " <> bar)
# on (SProxy :: SProxy "baz") (\baz -> "Baz: " <> show baz)
#match Source
match :: forall b r2 r1 r rl. RowToList r rl => VariantMatchCases rl r1 b => Union r1 () r2 => Record r -> Variant r2 -> b
Combinator for exhaustive pattern matching using an onMatch
case record.
matchFn :: Variant (foo :: Int, bar :: String, baz :: Boolean) -> String
matchFn = match
{ foo: \foo -> "Foo: " <> show foo
, bar: \bar -> "Bar: " <> bar
, baz: \baz -> "Baz: " <> show baz
}
#default Source
default :: forall r a. a -> Variant r -> a
Combinator for partial matching with a default value in case of failure.
caseFn :: forall r. Variant (foo :: Int, bar :: String | r) -> String
caseFn = default "No match"
# on (SProxy :: SProxy "foo") (\foo -> "Foo: " <> show foo)
# on (SProxy :: SProxy "bar") (\bar -> "Bar: " <> bar)
#contract Source
contract :: forall f gt lt. Alternative f => Contractable gt lt => Variant gt -> f (Variant lt)
A Variant gt
can be cast to some Variant lt
, where lt
is is a subset
of gt
, as long as there is proof that the Variant
's runtime tag is
within the subset of lt
.
#VariantEqs Source
class VariantEqs (rl :: RowList) where
Members
variantEqs :: RLProxy rl -> List (VariantCase -> VariantCase -> Boolean)
Instances
VariantEqs Nil
(VariantEqs rs, Eq a) => VariantEqs (Cons sym a rs)
#VariantOrds Source
class VariantOrds (rl :: RowList) where
Members
variantOrds :: RLProxy rl -> List (VariantCase -> VariantCase -> Ordering)
Instances
VariantOrds Nil
(VariantOrds rs, Ord a) => VariantOrds (Cons sym a rs)
#VariantShows Source
class VariantShows (rl :: RowList) where
Members
variantShows :: RLProxy rl -> List (VariantCase -> String)
Instances
VariantShows Nil
(VariantShows rs, Show a) => VariantShows (Cons sym a rs)
Re-exports from Data.Symbol
Re-exports from Data.Variant.Internal
#Contractable Source
class Contractable gt lt
Instances
(RowToList lt ltl, Union lt a gt, VariantTags ltl) => Contractable gt lt
#VariantMatchCases Source
class VariantMatchCases (rl :: RowList) (vo :: Row Type) b | rl -> vo b
Instances
(VariantMatchCases rl vo' b, RowCons sym a vo' vo, TypeEquals k (a -> b)) => VariantMatchCases (Cons sym k rl) vo b
VariantMatchCases Nil () b