Use DeriveGenerics, and higgledy (or some packages supporting higher kinded data) would help you.

2019年10月4日(金) 5:56 Sebastiaan Joosten <sjcjoosten+haskelcafe@gmail.com>:
Hi all,

I'm writing a lot of code that looks like this:
instance WhiteSpaced ClassItem where
  removeWS (Method a b c) = Method (removeWS a) (removeWS b) (removeWS c)
  removeWS (Declaration b) = Declaration (removeWS b)

Typically, all the way at the end there's an instance that deviates (sometimes the deviating instances are somewhere in the middle). I need to do this for a lot of functions, and a lot of data types, and all I'm doing here is rewriting the data-type declaration in a different syntax (except that you do not know the types of a, b and c from the above). For the sake of maintainability, I want to avoid this code-duplication and focus only on the deviating instances.

How to do better? I don't see how to use generics (in the hope of only writing 'instance WhiteSpaced ClassItem where' instead of the three lines above) for this: the types for a, b and c are all different here. Would this be easier with Template Haskell? (in the hope of only writing $(''something ClassItem) instead of the three lines above)

My main concern is maintainability, an ideal solution is either a clear one-liner or a library import (in the same way that aeson allows me to use generics or Template Haskell without needing to know much about them). Other solutions are welcome too.

Best,

Sebastiaan
_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.


--