cdornan / fmt

New formatting library
BSD 3-Clause "New" or "Revised" License
56 stars 6 forks source link

Add default implemention of build for Buildable #10

Closed chshersh closed 3 years ago

chshersh commented 6 years ago

Very often you want to write just

instance Buildable MyType where
    build = genericF

It may be better to have default implementation of build (which is not possible, because we don't use our own Buildable). But still things should be easier...

neongreen commented 6 years ago

I'm starting to think we should just use our own Buildable, with a fallback to Formatting.Buildable if there's no instance.

neongreen commented 6 years ago

Or even a fallback to Show.

dcastro commented 3 years ago

An alternative solution that wouldn't require having our own Buildable would be to leverage DerivingVia, e.g.:

newtype GenericBuildable a = GenericBuildable a

instance (GBuildable (Rep a), Generic a) => Buildable (GenericBuildable a) where
  build (GenericBuildable a) = genericF a

Usage:

data D = D
  deriving stock Generic
  deriving Buildable via GenericBuildable D

@neongreen @cdornan what do you think? I'd be happy to create a PR.

cdornan commented 3 years ago

@dcastro I like the DerivingVia idea : nondisruptive and really gets the job done. Feel free to raise a PR. (Sorry for the delay — other things have been sucking up available bandwidth.)

dcastro commented 3 years ago

@cdornan Thanks for getting back to me! No need to apologise, OSS is draining and life is wild, I completely understand :smile: I'll try to raise a PR in the coming weeks then, cheers!

cdornan commented 3 years ago

Fixed by #38