cardano-cli
Safe HaskellNone
LanguageHaskell2010

Cardano.CLI.Vary.Core

Synopsis

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 .

Constructors

Vary !Word (Any :: Type) 

Instances

Instances details
(Exception e, Exception (Vary errs), Typeable errs) => Exception (Vary (e ': errs)) Source #

See Vary and Exceptions for more info.

Instance details

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 # 
Instance details

Defined in Cardano.CLI.Vary.Core

GenericHelper (a ': as) => Generic (Vary (a ': as)) Source #

Any non-empty Vary's generic representation is encoded similar to a tuple but with :+: instead of :*:.

Instance details

Defined in Cardano.CLI.Vary.Core

Associated Types

type Rep (Vary (a ': as)) 
Instance details

Defined in Cardano.CLI.Vary.Core

type Rep (Vary (a ': as))

Methods

from :: Vary (a ': as) -> Rep (Vary (a ': as)) x Source #

to :: Rep (Vary (a ': as)) x -> Vary (a ': as) Source #

Generic (Vary ('[] :: [Type])) Source #

Vary '[] 's generic representation is V1.

Instance details

Defined in Cardano.CLI.Vary.Core

Associated Types

type Rep (Vary ('[] :: [Type])) 
Instance details

Defined in Cardano.CLI.Vary.Core

type Rep (Vary ('[] :: [Type]))

Methods

from :: Vary ('[] :: [Type]) -> Rep (Vary ('[] :: [Type])) x Source #

to :: Rep (Vary ('[] :: [Type])) x -> Vary ('[] :: [Type]) Source #

(Typeable a, Show a, Show (Vary as)) => Show (Vary (a ': as)) Source #

Vary's Show instance only works for types which are Typeable

This allows us to print the name of the type which the current value is of.

>>> Vary.from @Bool True :: Vary '[Int, Bool, String]
Vary.from @Bool True
>>> Vary.from @(Maybe Int) (Just 1234) :: Vary '[Maybe Int, Bool]
Vary.from @(Maybe Int) (Just 1234)
Instance details

Defined in Cardano.CLI.Vary.Core

Methods

showsPrec :: Int -> Vary (a ': as) -> ShowS Source #

show :: Vary (a ': as) -> String Source #

showList :: [Vary (a ': as)] -> ShowS Source #

Show (Vary ('[] :: [Type])) Source # 
Instance details

Defined in Cardano.CLI.Vary.Core

Methods

showsPrec :: Int -> Vary ('[] :: [Type]) -> ShowS Source #

show :: Vary ('[] :: [Type]) -> String Source #

showList :: [Vary ('[] :: [Type])] -> ShowS Source #

(NFData a, NFData (Vary as)) => NFData (Vary (a ': as)) Source # 
Instance details

Defined in Cardano.CLI.Vary.Core

Methods

rnf :: Vary (a ': as) -> () Source #

NFData (Vary ('[] :: [Type])) Source # 
Instance details

Defined in Cardano.CLI.Vary.Core

Methods

rnf :: Vary ('[] :: [Type]) -> () Source #

(Eq a, Eq (Vary as)) => Eq (Vary (a ': as)) Source # 
Instance details

Defined in Cardano.CLI.Vary.Core

Methods

(==) :: Vary (a ': as) -> Vary (a ': as) -> Bool Source #

(/=) :: Vary (a ': as) -> Vary (a ': as) -> Bool Source #

Eq (Vary ('[] :: [Type])) Source # 
Instance details

Defined in Cardano.CLI.Vary.Core

Methods

(==) :: Vary ('[] :: [Type]) -> Vary ('[] :: [Type]) -> Bool Source #

(/=) :: Vary ('[] :: [Type]) -> Vary ('[] :: [Type]) -> Bool Source #

(Ord a, Ord (Vary as)) => Ord (Vary (a ': as)) Source # 
Instance details

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 # 
Instance details

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 # 
Instance details

Defined in Cardano.CLI.Vary.Core

type Rep (Vary (a ': as))
type Rep (Vary ('[] :: [Type])) Source # 
Instance details

Defined in Cardano.CLI.Vary.Core

type Rep (Vary ('[] :: [Type]))

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:

  • Use Vary.morph to turn Vary as back into Vary (a : as)
  • Use Vary.from to turn a back into Vary (a : as)