Closed recursion-ninja closed 4 years ago
This is conflating some different issues. The examples of the form:
data Foo = A | B | C
deriving stock (Eq, Ord, Show)
are from the DerivingStrategies
language extension. In newer versions of hlint (after hlint-2-1-25) this can be enabled as a warning and I have done so here. This commit adds the particular deriving strategy we have used for all newtype declarations in our code base and adds a few easy-to-spot instances that can be derived rather than written out explicitly.
DerivingVia
goes one step further and allows one to do something like the following:
newtype Index = Index {getIndex :: Int}
deriving Monoid (via Sum Int)
I can imagine this being quite useful in our code base but it is much less mechanical to figure out where it can be employed.
Yes, this is what I intended. I thought DerivingVia
& DerivingStrategies
were a single language pragma.
At some point (it's low priority) we should look for the less mechanical instances that could be replaced with deriving via
.
We can add the new -Wmissing-deriving-strategies
flag once our code compiler with GHC 8.8.1.
This has been completed. See d219f1f2c12cd967497585bc27b5a28778b67c5d.
Nice W
The
DerivingVia
language extension allows for a much more descriptivederiving
statement, and in some cases, can remove the need for explicit type-classinstance
declarations.The following example shows how by using the
DerivingVia
style ofderiving
statement, it is clear how the instance below is derived from the GHC stock deriving mechanism:The following example shows similarly how we can specify deriving instances by coercing a newtype:
We should replace all the existing
deriving
statements in our codebase with the newDerivingVia
style for improved clarity of the deriving mechanism used.Additionally, we should inspect existing explicit type-class
instance
declarations for opportunities to remove tedious wrapping & unwrapping and/or coercion ofnewtype
s and replace the the instance with aderiving via
mechanism.