|
|
|
|
|
by premium-concern
3657 days ago
|
|
The spec mandates that type class instances can only be defined together with the type class or with the corresponding data type. While it's a very poor idea as no person except the author of the type class or the author of the data type can define a type class instance, it at least guarantees the global uniqueness and coherence of type class instances. ("There can only ever be a single typeclass instance for typeclass A and data type B.") GHC (the only implementation that kind of matters today) takes a YOLO approach, allows as many typeclass instances as you want, doesn't bother enforcing the rules laid out in the spec.
Additionally, as type class instances are not first-class values of the language a developer doesn't even have the option to make sure he is getting the instance he expects. There exists a GHC warning for "orphan type class instances"–but as the warning triggers on everything, not only the relevant cases where multiple instances clash and break the program–it's as useful as saying "we made sure that C's goto-fail bug can never happen again by emitting a warning for every suspicions indentation – you only have to sift through 1732 warnings until you find the dangerous one!". |
|
It's true that you can't safely define orphan instances in a library, but that also should involve no "sifting through" warnings for valid uses. It is genuinely bad practice to put orphan instances in a library, even if we had global checking - it would produce cases where you simply cannot use otherwise compatible libraries in the same project.