▲ | kreetx 3 days ago | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
For context, Haskell's story for orphan instances is currently as follows: - orphan instances are allowed and emit a warning - duplicate instances are not allowed - overlapping instances where one is different from the other, are allowed - incoherent instance use sites are not allowed (where 2+ instances match and neither is more specific than the other) - but you can enable this by adding {-# INCOHERENT #-} to instances. You shouldn't do this though unless you really know why you need it (and perhaps even then there is a better way) - a typical library sets all warnings as errors with -Wall, so you'll notice when you're adding orphans - exceptions in specific files can be made by adding -fno-orphans to the file - defining orphan instances in executables is not a problem as the only user of them will be the program itself - this is what you do if you are writing a package which only provides instances: where both the data types and the type classes are implemented elsewhere and you have no other choice. These libraries should not be used in other libraries, but only in executables and tests - a different instance can also be defined by wrapping the original type with a newtype (thus defining that new instance for this new type, thus not making an orphan) - since newtypes have no runtime overhead, also, with DerivingVia, syntactic overhead is quite low. This is "the way" to override already defined instances. IMO, all the above makes sense when you prefer correctness over flexibility. From the post, this appears to be Rust's choice as well. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
▲ | klabb3 3 days ago | parent [-] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
The newtype pattern is a special case of type composition which is incredibly useful, has low complexity, and if done right almost no boilerplate overhead. It's much dumber and easier to reason about than type-acrobatics with generics, imo. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|