haskell - 有一个干净的导入块的方法? (重新导出模块?每行有多个进口?) [英] haskell -- ways to have a clean import block? (re-exporting modules? multiple imports per line?)

查看:154
本文介绍了haskell - 有一个干净的导入块的方法? (重新导出模块?每行有多个进口?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常习惯于有一个标准的导入区块,所以当我需要时我常常使用功能。例如,

   - 许可证块

{ - #LANGUAGE箭头,
DeriveDataTypeable ,
EmptyDataDecls,
FlexibleContexts,
FlexibleInstances,
FunctionalDependencies,
GADTs,
MultiParamTypeClasses,
NoMonomorphismRestriction,
RankNTypes,
ScopedTypeVariables,
StandaloneDeriving,
TypeOperators,
TypeSynonymInstances,
UndecidableInstances,
ViewPatterns# - }

module MyModule其中

导入前导隐藏(id,(。))
导入Control.Arrow
导入Control.Category $ b $导入Control.Exception $ b $导入Control.Monad
导入Control.Monad.ST

导入Data.Array.Diff
将限定的Data.Map导入为映射
将限定的Data.Set导入为集合
导入限定Data.Heap as Heap
im端口限定的Data.List作为列表
导入限定的Data.List.Key作为键
导入Data.List.HT
导入Data.Maybe
导入Data.STRef
将合格的Data.Text导入为T

因为我没有使用任何花哨的IDE,我宁愿不要在每个文件中阅读此内容。是否有创建某种标准/实用程序模块的方式来重新导出这些函数的名称,所以我可以输入

  import MyCommonFuncs 

改为

我知道这可能不是一个好的做法,理论上可能应该增加必要的最小进口数量,但正如我所说的,我有时需要破解,而且要考虑的东西总是不错的。

编辑 - 更重要的是,有时我想更改所有文件的块,所以当前系统单独导入所有东西可能是单调乏味的。例如,假设我需要删除旧的 import Time 并将其更改为新的[sic?] import System.Time 。然后,在当前系统下,我必须编辑所有文件。或者,也许我了解了一种新的语言功能,并希望始终可用 - 那么我必须手动更新旧的源文件。

解决方案您可以尝试使用语法

  module MyCommonFuncs(
module Control.Arrow,
模块Control.Category,
模块Control.Exception,
模块Control.Monad,
模块Control.Monad.ST


导入控件。 Arrow
import Control.Category
import Control.Exception
import Control.Monad
import Control.Monad.ST

当你这样做时,这些模块中的所有函数都会被导出,就像在模块 MyCommonFuncs 本身中定义的一样。 / p>

至于 LANGUAGE 编译指示,您可以在 .cabal 文件,它们全局使用。只是不是在通过 ghci 进行测试。



希望这有助于您。


I'm often in the habit of having a standard import block, so I have commonly used functionality at hand when I need it. For example,

-- license block

{-# LANGUAGE Arrows,
            DeriveDataTypeable,
            EmptyDataDecls,
            FlexibleContexts,
            FlexibleInstances,
            FunctionalDependencies,
            GADTs,
            MultiParamTypeClasses,
            NoMonomorphismRestriction,
            RankNTypes,
            ScopedTypeVariables,
            StandaloneDeriving,
            TypeOperators,
            TypeSynonymInstances,
            UndecidableInstances,
            ViewPatterns #-}

module MyModule where

import Prelude hiding (id, (.))
import Control.Arrow
import Control.Category
import Control.Exception
import Control.Monad
import Control.Monad.ST

import Data.Array.Diff
import qualified Data.Map as Map
import qualified Data.Set as Set
import qualified Data.Heap as Heap
import qualified Data.List as List
import qualified Data.List.Key as Key
import Data.List.HT
import Data.Maybe
import Data.STRef
import qualified Data.Text as T

Since I'm not using any fancy IDE's, I'd prefer not to read this in every file. Is there a way to create some kind of "standard" / "utility" module that re-exports the names of these functions, so I can just type,

import MyCommonFuncs

instead?

I know this is maybe not good practice, and maybe in theory one should add the minimial number of imports necessary, but as I said, I need to hack sometimes, and having fewer things to think about is always good.

EDIT -- more importantly, sometimes I want to change the block for all files, so the current system of importing everything individually can be tedious. Suppose, for example, that I need to remove the old import Time and change this to the new [sic?] import System.Time. Then, with the current system, I have to edit all files. Or, maybe I learn about a new language feature, and want that always available -- then I have to go manually update the older source files.

解决方案

You can try using the Syntax

module MyCommonFuncs (
    module Control.Arrow,
    module Control.Category,
    module Control.Exception,
    module Control.Monad,
    module Control.Monad.ST
)

import Control.Arrow
import Control.Category
import Control.Exception
import Control.Monad
import Control.Monad.ST

When you do this, all the functions in these modules are exported as if defined in the module MyCommonFuncs itself.

As for the LANGUAGE pragma, you can define the one you want in the .cabal file, they are used globally. Just not when testing via ghci.

Hope this helped.

这篇关于haskell - 有一个干净的导入块的方法? (重新导出模块?每行有多个进口?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆