snoyberg / mono-traversable

Type classes for mapping, folding, and traversing monomorphic containers
153 stars 63 forks source link

Automatic derivation of MonoFunctor for newtypes? #96

Closed deepfire closed 8 years ago

deepfire commented 8 years ago

newtype is cool for increasing type safety, but one has to pay the price of wrapping-rewrapping for mapping over them. For polymorphic newtypes, one can derive Functor, which is very cheap, text-wise -- just an addition of Functor into the deriving clause.

For monomorphic newtypes we only have MonoFunctor, but it cannot be derived, so it becomes the rather cumbersome:

newtype Wrap = Wrap Int
type instance Element Wrap = Int
instance MonoFunctor Wrap where
    omap f (Wrap x) = Wrap (f x)

So the question is -- can we employ DeriveAnyClass or some other technique to allow automatic derivation of MonoFunctor?

snoyberg commented 8 years ago

Have you tried GeneralizedNewtypeDeriving? I don't know if that will work with the default methods already provided.

On Fri, Apr 29, 2016 at 3:35 AM, Kosyrev Serge notifications@github.com wrote:

newtype is cool for increasing type safety, but one has to pay the price of wrapping-rewrapping for mapping over them. For polymorphic newtypes, one can derive Functor, which is very cheap -- just an addition of Functor into the deriving clause.

For monomorphic newtypes we only have MonoFunctor, but it cannot be derived, so it becomes the rather cumbersome:

newtype Wrap = Wrap Int type instance Wrap = Int instance MonoFunctor Wrap where omap f (Wrap x) = Wrap (f x)

So the question is -- can we employ DeriveAnyClass or some other technique to allow automatic derivation of MonoFunctor?

— You are receiving this because you are subscribed to this thread. Reply to this email directly or view it on GitHub https://github.com/snoyberg/mono-traversable/issues/96

deepfire commented 8 years ago

Disregard the previous comment, as I was trying to define an instance in stead of deriving it.