| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Data.Diverse.Cases
- data Cases fs r xs
- cases :: forall r xs fs. (AllConstrained ((~) r) (CaseResults (Cases fs r) fs), SameLength fs (Nub xs)) => Many fs -> Cases fs r xs
- cases' :: forall r xs fs. AllConstrained ((~) r) (CaseResults (Cases fs r) fs) => Many fs -> Cases fs r xs
- data CasesN fs r n xs
- casesN :: forall r xs fs. (AllConstrained ((~) r) (CaseResults (CasesN fs r 0) fs), SameLength fs xs) => Many fs -> CasesN fs r 0 xs
- casesN' :: forall r xs fs. AllConstrained ((~) r) (CaseResults (CasesN fs r 0) fs) => Many fs -> CasesN fs r 0 xs
Documentation
Contains a Many of handlers/continuations for all the types in the xs typelist.
This uses fetch to get the unique handler for the type at the Head of xs.
Use cases to construct this with SameLength constraint to reduce programming confusion.
cases :: forall r xs fs. (AllConstrained ((~) r) (CaseResults (Cases fs r) fs), SameLength fs (Nub xs)) => Many fs -> Cases fs r xs Source #
Create an instance of Case for either handling switching a Which.
let y =pick(5 :: Int) ::Which'[Int, Bool]switchy (cases(show @Bool./show @Int./nul)) `shouldBe` "5"
Or for handling collect from a Many.
let x = (5 :: Int)./False./'X'./Just 'O'./(6 :: Int)./Just 'A'./nuly = show @Int./show @Char./show @(Maybe Char)./show @Bool./nulafoldr(:) [] (collectx (casesy)) `shouldBe` ["5", "False", "X", "Just 'O'", "6", "Just 'A'"]
This function imposes additional SameLength constraints than when using the Cases constructor directly.
It is better practice to use cases to prevent programming confusion with dead code.
However, the Cases constructor is still exported to allow creating a master-of-all-Case.
cases' :: forall r xs fs. AllConstrained ((~) r) (CaseResults (Cases fs r) fs) => Many fs -> Cases fs r xs Source #
casesN :: forall r xs fs. (AllConstrained ((~) r) (CaseResults (CasesN fs r 0) fs), SameLength fs xs) => Many fs -> CasesN fs r 0 xs Source #
Safe Constructor for CasesN ensuring that the n Nat starts at 0.
It is an instance of CaseN for either handling switchNing a Which in index order.
let y =pickN@0 Proxy (5 :: Int) :: Which '[Int, Bool, Bool, Int]switchNy (casesN(show @Int./show @Bool./show @Bool./show @Int./nul)) `shouldBe` "5"
Or for handling collectN from a Many.
let x = (5 :: Int)./False./'X'./Just 'O'./(6 :: Int)./Just 'A'./nuly = show @Int./show @Bool./show @Char./show @(Maybe Char)./show @Int./show @(Maybe Char)./nulafoldr(:) [] (collectNx (casesNy)) `shouldBe` ["5", "False", "X", "Just 'O'", "6", "Just 'A'"]
casesN' :: forall r xs fs. AllConstrained ((~) r) (CaseResults (CasesN fs r 0) fs) => Many fs -> CasesN fs r 0 xs Source #