generic-override icon indicating copy to clipboard operation
generic-override copied to clipboard

Separate overriding (`Override`) and generic (`Generically`) behaviour

Open Icelandjack opened this issue 2 years ago • 0 comments

Soon Generically and Generically1 will be added to base 4.17, I notice that you implement generic instances for Override that duplicates the intended use of Generically.

It is possible to pass Override as an argument to Generically that makes use of the modified Generic instance of Override:

-- >> mempty @Ok
-- Ok 0 1
-- >> Ok 10 10 <> Ok 10 10
-- Ok 20 100
data Ok = Ok Int Int
  deriving (Semigroup, Monoid)
  via Generically
    (Override Ok
        '[ At "Ok" 0 (Sum Int)
         , At "Ok" 1 (Product Int)
         ]
    )

Using this pattern you separate the generic behaviour from the overriding behaviour, which will be added separately by many libraries. You could possibly define a type level constant

type Overriding :: Type -> [Type] -> Type
type Overriding a spec = Generically (Override a spec)

Icelandjack avatar Mar 17 '22 19:03 Icelandjack