Haskell Re-Export Module

by|inArticles||1 min read
Haskell Re-Export Module<br>
Haskell Re-Export Module<br>

Haskell Imports can be a bit overwhelming with time. Writing imports over and over again is no fun neither. A common technique is the import and re-export of one or multiple modules.

Let's imagine we created a project with multiple modules, just for simple records, each file defining one module and a couple of records in each file.

Usually, if you go this path, you will end up with a lot of boilerplate code for imports:

module App.Handler where

import App.Request.ArticleRequest
import App.Response.ArticleResponse
import App.Request.ProjectRequest
import App.Response.ProjectResponse
import App.Request.CustomerRequest
import App.Response.CustomerResponse
-- etc, etc.

Luckily Haskell allows to "group" similar modules into one module, hence re-export modules:

module App.Request
  ( module App.Request.ArticleRequest
  , module App.Request.ProjectRequest
  , module App.Request.CustomerRequest
  )

import App.Request.ArticleRequest
import App.Request.ProjectRequest
import App.Request.CustomerRequest

This way we can use the new Request module to import all types of request records just in one line.

module App.Handler where

import App.Request
import App.Response.ArticleResponse
import App.Response.ProjectResponse
import App.Response.CustomerResponse
-- etc, etc.

We could do the same as well for the response records and end up with the following import within a using module:

module App.Handler where

import App.Request
import App.Response

This is already much more readable and a pleasure to write. If you don't need all modules and just want specific records, we can do this as well:

module App.Handler where

import App.Request (CustomerPostRequest(..), CustomerPutRequest(..))
import App.Response (CustomerResponse(..))

Thank you for reading this far! Let’s connect. You can @ me on X (@debilofant) with comments, or feel free to follow. Please like/share this article so that it reaches others as well.

Related Articles

© Copyright 2024 - ersocon.net - All rights reservedVer. 415