data-diverse-0.4.0.0: Extensible records and polymorphic variants.

Safe HaskellNone
LanguageHaskell2010

Data.Diverse.Cases

Synopsis

Documentation

newtype Cases fs xs r Source #

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. However, the Cases constructor is still exported to allow creating a master-of-all-Case.

Constructors

Cases (Many fs) 

Instances

UniqueMember * (Head * xs -> r) fs => Case (Cases fs) xs r Source #

UndecidableInstances because fs appers more often.

Methods

case' :: Cases fs xs r -> Head Type xs -> r Source #

Reiterate * (Cases fs) xs Source # 

Methods

reiterate :: xs xs r -> xs (Tail Type xs) r Source #

cases :: SameLength fs (Nub xs) => Many fs -> Cases fs xs r Source #

Create an instance of Case for either handling switching a Which.

let y = pick (5 :: Int) :: Which '[Int, Bool]
switch y (
    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' ./ nul
    y = show @Int ./ show @Char ./ show @(Maybe Char) ./ show @Bool ./ nul
afoldr (:) [] (collect x (cases y)) `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.

data CasesN fs n xs r Source #

A variation of Cases which uses fetchN to get the handler by index. There may be different handlers for the same type, but the handlers must be in the same order as the input xs typelist. Use casesN to construct this safely ensuring n starts at 0.

Instances

ReiterateN * (CasesN fs) n xs Source # 

Methods

reiterateN :: n xs xs r -> n (xs + 1) (Tail Type xs) r Source #

MemberAt * n (Head * xs -> r) fs => Case (CasesN fs n) xs r Source #

UndecidableInstances because fs appears more often.

Methods

case' :: CasesN fs n xs r -> Head Type xs -> r Source #

casesN :: SameLength fs xs => Many fs -> CasesN fs 0 xs r 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]
switchN y (
    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' ./ nul
    y = show @Int ./ show @Bool ./ show @Char ./ show @(Maybe Char) ./ show @Int ./ show @(Maybe Char) ./ nul
afoldr (:) [] (collectN x (casesN y)) `shouldBe`
    ["5", "False", "X", "Just 'O'", "6", "Just 'A'"]