3

If I have a bunch of modules in a directory called Views, I'd like to import them all with import Views.*. Instead, as far as I can tell the current haskell idiom is to create a file Views.hs that imports all those files.

My use case:

I'm building a web app with Scotty. My views are written in blaze-html and I import and render them like this:

import Views.Posts.Index

scotty 3000 $ do
  get "/" $ do
    posts <- getPosts
    blaze $ Views.Posts.Index.render posts

So if I have 10 views, I need to import each one explicitly...and when I create a view I need a new import before I can use it. Compare this to mustache:

scotty 3000 $ do
  get "/" $ do
    mustache "views/posts/index"
beresfordt
  • 5,088
  • 10
  • 35
  • 43
Vlad the Impala
  • 15,572
  • 16
  • 81
  • 124
  • 1
    It's probably because wildcard imports can lead to ambiguous references to functions. Also, wildcards - in general - can lead to code that isn't robust e.g. using `_` in a pattern match will mean you won't get warnings if you missed an important case. Likewise, using a wildcard for imports means that it isn't explicit in your code exactly what modules you are depending on or what is being imported. – Peter Hall Mar 31 '13 at 18:11
  • 8
    You `import Views.*`. Then you install an unrelated library that has a `module Views.You.Didn't.Expect`. Oops. – Daniel Fischer Mar 31 '13 at 18:16
  • @DanielFischer good point! How about allowing relative imports only, like Ruby's require_relative? `relative_import Views.*` – Vlad the Impala Mar 31 '13 at 20:19
  • 3
    Relative meaning from the package for a library, and from the current source tree for non-package compilations? Could work, although it might be a bit complicated to define the correct desired semantics. Regarding the question in the title, history, I think. Haskell98 had no hierarchical modules, so there wasn't even the possibility to `import Views.*`. Then nobody proposed and argued for such a feature, so it wasn't added. – Daniel Fischer Mar 31 '13 at 20:35
  • 1
    Such a feature could be implemented safe using the PackageImports extension of GHC. See http://hackage.haskell.org/trac/ghc/wiki/Commentary/Packages/PackageImportsProposal?redirectedfrom=PackageImports (so, people think about things like this, it just hasn't landed, which tells me theres not that great of a need for it in the Haskell community) – scravy Apr 01 '13 at 12:13

0 Answers0