JonathanLorimer / cfg

Simple Haskell configuration library
MIT License
3 stars 0 forks source link

Better error message when not importing newtype constructor for deriving-via #13

Closed JonathanLorimer closed 1 year ago

JonathanLorimer commented 1 year ago

For the deriving via machinery here:

data SubTyCon = SubDataCon
  { subKey1 :: Text
  , subKey2 :: Int
  , subKey3 :: Maybe Bool
  }
  deriving (Generic, Show, Eq)
  deriving (NestedParser) via (SubConfig SubTyCon)

We get this error when the constructor for SubConfig isn't imported

   • Couldn't match representation of type ‘SubConfig SubTyCon’
                              with that of ‘SubTyCon’
       arising from a use of ‘ghc-prim-0.10.0:GHC.Prim.coerce’
       The data constructor ‘cfg-0.0.1.0:Cfg.Deriving.SubConfig.SubConfig’
         of newtype ‘SubConfig’ is not in scope
   • In the expression:
       ghc-prim-0.10.0:GHC.Prim.coerce
         @(Tree Text -> Either ConfigParseError (SubConfig SubTyCon))
         @(Tree Text -> Either ConfigParseError SubTyCon)
         (parseNestedConfig @(SubConfig SubTyCon))

The error is because of this coerce that I am doing (a trick I took from the deriving-aeson library)

instance (Generic a, GNestedParser (Rep a)) => NestedParser (SubConfig a) where
  parseNestedConfig tree = coerce `asTypeOf` fmap SubConfig $ defaultParseNestedConfig defaultConfigOptions tree

It would be nice to either: a) prevent this error altogether b) provide a nicer error message

JonathanLorimer commented 1 year ago

Seems like this is the case for all deriving via, just need good documentation.