[Haskell-cafe] Re: Unwrapping newtypes
kevinjardine at gmail.com
Wed Sep 8 08:19:17 EDT 2010
I stared at that specific section for at least half an hour earlier
today but could not figure out how it applied in my specific case. The
only examples I have see are for deriving Num. Do you have any more
detail on how I could use that extension?
On Sep 8, 2:05 pm, Tony Morris <tonymor... at gmail.com> wrote:
> I think you might want -XGeneralizedNewtypeDeriving
> On 08/09/10 22:01, Kevin Jardine wrote:
> > I have a generic object that I want to wrap in various newtypes to
> > better facilitate type checking.
> > For example,
> > newtype Blog = Blog Obj
> > newtype Comment = Comment Obj
> > newtype User = User Obj
> > Unlike Obj itself, whose internal structure is hidden in a library
> > module, the newtype wrappings are purely to facilitate type checking.
> > It is no secret that each is just a wrapper around Obj.
> > It is obvious how to construct the various wrapper objects. It is not
> > so obvious how to extract the Obj they contain in a reasonably generic
> > way however. What I want is a getObj function that works on all of
> > them.
> > Of course this could work if someone using the library wrote an
> > instance for each wrapper object:
> > instance GetObject Blog where
> > getObj (Blog obj) = obj
> > but this is a pain in the neck to write for each newtype.
> > I discovered that Foldable defines a handy toList function that
> > extracts content from generic Foldable structures.
> > So that I could write:
> > toObj :: Foldable thing => thing Obj -> Obj
> > toObj w = head $ toList w
> > Slightly kludgy but it works.
> > Even better, recent versions of GHC will allow you to automatically
> > derive Foldable.
> > Unfortunately,
> > newtype Blog = Blog Obj deriving Foldable
> > returns a kind error.
> > What does work is:
> > newtype BlogF a = Blog a deriving Foldable
> > type Blog = BlogF Obj
> > After having spent close to a day on this, I am a bit baffled that
> > such a seemingly trivial problem seems so hard to do.
> > I am wondering if I am missing something really, really obvious.
> > Any suggestions? Or is there perhaps a more Haskelly way to place type
> > constraints on a more generic type?
> > Kevin
> > _______________________________________________
> > Haskell-Cafe mailing list
> > Haskell-C... at haskell.org
> Tony Morrishttp://tmorris.net/
> Haskell-Cafe mailing list
> Haskell-C... at haskell.orghttp://www.haskell.org/mailman/listinfo/haskell-cafe
More information about the Haskell-Cafe