Daniil Iaitskov
Daniil Iaitskov

Reputation: 6039

How to import newtype instance

I extracted persist code generating record types into a separate module (Dao) and I want to keep imports clean and strict so I tried to import explicitly all types and functions, I need, from Dao module. I stuck with newtype instance for Key. Key is not my type family. It is defined in persist library.

import Dao -- work but it is a mystery how much is imported

GHC (8.6.5) is looking pretty smart and even trying to help with my struggle:

    In module `Dao':
      `RedirectMappingRKey' is a data constructor of `Key'
    To import it use
      import Dao( Key( RedirectMappingRKey ) )
    or
      import Dao( Key(..) )
   |
52 | import Dao (openDbPool, RedirectMappingR(..), RedirectMappingRKey)

but both version suggested by GHC is reject by it:

    Module `Dao' does not export `Key(RedirectMappingRKey)'
   |
52 | import Dao (openDbPool, RedirectMappingR(..), Key(RedirectMappingRKey))

or

C:\pro\demo\haskell\servant\myproject\src\Lib.hs:52:47: error: Module `Dao' does not export `Key(..)'
   |
52 | import Dao (openDbPool, RedirectMappingR(..), Key(..))
   |                                               ^^^^^^^

instance definition

*Dao> :i RedirectMappingRKey
newtype instance persistent-2.9.2:Database.Persist.Class.PersistEntity.Key
                   RedirectMappingR
  = RedirectMappingRKey {...}

I run out of ideas, probably qualified import could help me, but I would like to know the best solution.

Upvotes: 1

Views: 372

Answers (1)

bradrn
bradrn

Reputation: 8467

Key is not my type family. It is defined in persist library

Then you can’t import it from Dao, unless Dao specifically lists Key in its export list (see https://taylor.fausak.me/2016/12/30/automatically-export-haskell-modules/). In order to use Key, you must add the persistent library as a dependency, then do import Database.Persist.Class (Key(..)) to import it from persistent.

Upvotes: 4

Related Questions