Safe Haskell | None |
---|---|
Language | Haskell2010 |
Cardano.CLI.Vary.Core
Documentation
data Vary (possibilities :: [Type]) Source #
Vary, contains one value out of a set of possibilities
Vary is what is known as a Variant type. This is also known as an open union or coproduct, among other names.
You can see it as the generalization of Either
.
Conceptually, these are the same:
Vary [a, b, c, d, e] Either a (Either b (Either c (Either d e)))
However, compared to a deeply nested Either
, Vary
is:
- Much easier to work with;
- Much more efficient, as a single (strict) word is used for the tag.
Vary
's can be constructed with Vary.from
and values can be extracted using Vary.into
and Vary.on
.
Instances
(Exception e, Exception (Vary errs), Typeable errs) => Exception (Vary (e ': errs)) Source # | See Vary and Exceptions for more info. |
Defined in Cardano.CLI.Vary.Core Methods toException :: Vary (e ': errs) -> SomeException Source # fromException :: SomeException -> Maybe (Vary (e ': errs)) Source # displayException :: Vary (e ': errs) -> String Source # | |
(Typeable (Vary ('[] :: [Type])), Show (Vary ('[] :: [Type]))) => Exception (Vary ('[] :: [Type])) Source # | |
Defined in Cardano.CLI.Vary.Core Methods toException :: Vary ('[] :: [Type]) -> SomeException Source # fromException :: SomeException -> Maybe (Vary ('[] :: [Type])) Source # | |
GenericHelper (a ': as) => Generic (Vary (a ': as)) Source # | Any non-empty Vary's generic representation is encoded similar to a tuple but with |
Generic (Vary ('[] :: [Type])) Source # | Vary '[] 's generic representation is |
(Typeable a, Show a, Show (Vary as)) => Show (Vary (a ': as)) Source # |
This allows us to print the name of the type which the current value is of.
|
Show (Vary ('[] :: [Type])) Source # | |
(NFData a, NFData (Vary as)) => NFData (Vary (a ': as)) Source # | |
Defined in Cardano.CLI.Vary.Core | |
NFData (Vary ('[] :: [Type])) Source # | |
(Eq a, Eq (Vary as)) => Eq (Vary (a ': as)) Source # | |
Eq (Vary ('[] :: [Type])) Source # | |
(Ord a, Ord (Vary as)) => Ord (Vary (a ': as)) Source # | |
Defined in Cardano.CLI.Vary.Core Methods compare :: Vary (a ': as) -> Vary (a ': as) -> Ordering Source # (<) :: Vary (a ': as) -> Vary (a ': as) -> Bool Source # (<=) :: Vary (a ': as) -> Vary (a ': as) -> Bool Source # (>) :: Vary (a ': as) -> Vary (a ': as) -> Bool Source # (>=) :: Vary (a ': as) -> Vary (a ': as) -> Bool Source # max :: Vary (a ': as) -> Vary (a ': as) -> Vary (a ': as) Source # min :: Vary (a ': as) -> Vary (a ': as) -> Vary (a ': as) Source # | |
Ord (Vary ('[] :: [Type])) Source # | |
Defined in Cardano.CLI.Vary.Core Methods compare :: Vary ('[] :: [Type]) -> Vary ('[] :: [Type]) -> Ordering Source # (<) :: Vary ('[] :: [Type]) -> Vary ('[] :: [Type]) -> Bool Source # (<=) :: Vary ('[] :: [Type]) -> Vary ('[] :: [Type]) -> Bool Source # (>) :: Vary ('[] :: [Type]) -> Vary ('[] :: [Type]) -> Bool Source # (>=) :: Vary ('[] :: [Type]) -> Vary ('[] :: [Type]) -> Bool Source # max :: Vary ('[] :: [Type]) -> Vary ('[] :: [Type]) -> Vary ('[] :: [Type]) Source # min :: Vary ('[] :: [Type]) -> Vary ('[] :: [Type]) -> Vary ('[] :: [Type]) Source # | |
type Rep (Vary (a ': as)) Source # | |
Defined in Cardano.CLI.Vary.Core | |
type Rep (Vary ('[] :: [Type])) Source # | |
Defined in Cardano.CLI.Vary.Core |
pop :: forall a (as :: [Type]). Vary (a ': as) -> Either (Vary as) a Source #
Attempts to extract a value of the first type from the Vary
.
If this failed, we know it has to be one of the other possibilities.
This function can also be seen as turning one layer of Vary
into its isomorphic Either
representation.
This function is not often useful in normal
code, but super useful in generic code where you want to recurse on the variant's types.
For instance when implementing a typeclass for any Vary
whose elements implement the typeclass:
instance Show (Vary '[]) where show = Vary.exhaustiveCase instance (Show a, Show (Vary as)) => Show (Vary (a : as)) where show vary = case Vary.pop vary of Right val -> "Vary.from " <> show val Left other -> show other
To go the other way: