[Haskell-beginners] Newtype to avoid orphans
Brent Yorgey
byorgey at seas.upenn.edu
Mon Feb 6 17:40:33 CET 2012
The concept is simple: an orphan instance is an instance
instance Class Type ...
which is in neither the module where Class is declared, nor the module
where Type is declared. In your case, both 'Num' and 'Data.Vector'
are defined elsewhere. One reason you might not want this is because
many people might define identical or similar instances for the same
types, so if/when that instance is actually added to the module
defining the type or class in question it will break a lot of code.
But in my mind this is not an argument *against* orphan instances but
rather an argument *for* contributing instances back upstream (which
is certainly a good idea when it makes sense). For example if you
think the Num instance for Data.Vector is something that would be
generally useful you can submit a patch to the Data.Vector maintainer,
and then delete the instance from your module when a new version of
Data.Vector is released.
However, sometimes there are good reasons not to do this -- for
example if (say) the implementation of the Num instance for
Data.Vector relied on some *other* package and it was desirable not to
add this other package as a dependency for Data.Vector.
I seem to recall there is also something about GHC having to do more
work for orphan instances, but I don't recall the details. And I am
not sure why I should have to change my code to save GHC some work --
I want *GHC* to work harder to save *me* work, not the other way
around.
-Brent
On Mon, Feb 06, 2012 at 04:25:05PM +0100, Adrien Haxaire wrote:
> Thank you.
>
> It will save me some time, and I will be happy using the nice API of
> Data.Vector without having to wrap everything.
>
> I will still read more on orphan instances to be sure I understand
> the concept and the consequences, even if now I know they are minor
> for me.
>
> Adrien
>
>
> On Mon, 6 Feb 2012 08:53:14 -0500, Brent Yorgey wrote:
> >Yes: don't bother. Having an orphan instance is really not that big
> >of a deal. And it's certainly not worth making a newtype just to
> >avoid the warning. If you want to turn off the warning you can add
> >
> >{-# OPTIONS_GHC -fno-warn-orphans #-}
> >
> >to the top of your file.
> >
> >-Brent
> >
> >_______________________________________________
> >Beginners mailing list
> >Beginners at haskell.org
> >http://www.haskell.org/mailman/listinfo/beginners
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
More information about the Beginners
mailing list