1

I want to generate import qualified Aaaa.Bbb.Ccc as Ccc automatically at compile time.

Is there any way to do that? Maybe by Template Haskell or anyhow else with any extention? I think it's similar to macroses in C and the function $(...) in Tempalte Haskell.

Incerteza
  • 32,326
  • 47
  • 154
  • 261
  • 3
    What are you trying to achieve? Do you have some import statement you want to change for different package versions? – badcook Apr 21 '16 at 09:02
  • 1
    Probably will be useful Conditional Compilation https://www.haskell.org/cabal/users-guide/developing-packages.html#conditional-compilation – josejuan Apr 21 '16 at 09:18
  • You can enable the CPP preprocessor, as in C. Use `{-# LANGUAGE CPP #-}` at the top of your file, and `#define` or `#include` your macros. Still, I wonder if that is really necessary. – chi Apr 21 '16 at 12:11
  • TH cannot generate import statements, so you are stuck with the c preprocessor. – user2407038 Apr 21 '16 at 15:22

1 Answers1

3

If what you are trying to achieve is shortening your import list, you may try the following trick. Create a new module (Foo):

module Foo (module X) where
import A as X
import B as X
import C as X

Then import this module to get all members of A, B, and C:

module Bar where
import qualified Foo as X

This way you can combine modules which are commonly used throughout your project.

If you still need to auto-generate the imports, at least you only need to generate module Foo, but not Bar. So the automatically and manually generated code is separated cleanly.

sapanoia
  • 789
  • 4
  • 14