Closed MichaelXavier closed 1 year ago
Good question. I think one could use a higher-kinded version of the (&)
class in the constraints
package. Something like:
{-# LANGUAGE ConstraintKinds, UndecidableSuperClasses #-}
module Foo where
import GHC.Generics
import Barbies
class (c a, d a) => (c & d) a where
instance (c a, d a) => (c & d) a where
blah :: (Eq a, Show a, Enum a) => Maybe a -> [a]
blah = maybe [] pure
convert :: Foo Maybe -> Foo []
convert = bmapC @(Eq & Show & Enum) blah
data Foo f = Foo (f Int)
deriving Generic
deriving (FunctorB, ConstraintsB)
deriving instance AllBF Show f Foo => Show (Foo f)
Would that work for you?
That worked great, thank you! I had tried the one from the constraints
package but it wasn't clicking with me that I needed a fully applied version. It may be worth adding this somewhere in the docs and mentioning that you can use it in the constraint-oriented parts of the barbies API. IMO it would also be completely reasonable to actually define and export it in barbies itself if the language extensions aren't too problematic.
IMO it would also be completely reasonable to actually define and export it in barbies itself if the language extensions aren't too problematic.
Done in 2.0.5.0!
I've noticed in the code that the convenience functions like
bmapC
seem to let you specify a constraint via type applications likebmapC @Show
but I haven't found any examples of specifying multiple constraints such asShow
andEq
. I haven't really found a good solution for this because type applications doesn't seem to like specifying multiple constraints with one type application. Combining multiple constraints into a type alias with ConstraintKinds doesn't seem to work easier because they don't get partially applied in the same way that individual constraints do. I'm very hesitant to define an entirely new class just for a specific combination of 2 or more classes. Am I missing a better way to do this?