haskell——有什么方法可以推出你自己的语言编译指示组吗?
我有一个 Haskell 项目,经常使用很多语言功能,并且我希望每个源文件的语言扩展块都相同。这是一个列表,
{-# LANGUAGE Arrows,
BangPatterns,
DefaultSignatures,
DeriveDataTypeable,
DeriveFunctor,
EmptyDataDecls,
FlexibleContexts,
FlexibleInstances,
FunctionalDependencies,
GADTs,
GeneralizedNewtypeDeriving,
MultiParamTypeClasses,
NamedFieldPuns,
NoImplicitPrelude,
NoMonomorphismRestriction,
OverlappingInstances,
RankNTypes,
RebindableSyntax,
ScopedTypeVariables,
StandaloneDeriving,
TemplateHaskell,
TypeFamilies,
TypeOperators,
TypeSynonymInstances,
UndecidableInstances,
ViewPatterns #-}
也许对某些人来说这是不好的做法,但我认为语言扩展是我通常编写代码的“Haskell+”的一部分。而且,我希望跨模块的情况相同。例如,NoImplicitPrelude
极大地改变了语言,我希望它对所有模块都是统一的。
问题:如何实现此目的,而不将语言块复制粘贴到每个文件中?我经常学习一种新的语言功能,将其添加到模块 A
,然后开始处理模块 B
,并意识到我必须从其中复制语言块,这很烦人模块A
。
仅供参考,带有 #include
的 CPP
编译指示并不能解决问题!提前致谢。
I have a Haskell project that regularly uses a lot of language features, and I want the language extension block for each source file to be the same. Here's a list,
{-# LANGUAGE Arrows,
BangPatterns,
DefaultSignatures,
DeriveDataTypeable,
DeriveFunctor,
EmptyDataDecls,
FlexibleContexts,
FlexibleInstances,
FunctionalDependencies,
GADTs,
GeneralizedNewtypeDeriving,
MultiParamTypeClasses,
NamedFieldPuns,
NoImplicitPrelude,
NoMonomorphismRestriction,
OverlappingInstances,
RankNTypes,
RebindableSyntax,
ScopedTypeVariables,
StandaloneDeriving,
TemplateHaskell,
TypeFamilies,
TypeOperators,
TypeSynonymInstances,
UndecidableInstances,
ViewPatterns #-}
Maybe to some it's bad practice, but I consider language extensions to be part of the "Haskell+" that I usually write code in. And, I want that to be the same across modules. For example, the NoImplicitPrelude
changes the language dramatically, and I want it uniform for all modules.
Question: How can I achieve this, without copy-pasting the language block into each file? It gets annoying how I often learn a new language feature, add it to module A
, then start working on module B
, and realize I have to go copy the language block from module A
.
Just FYI the CPP
pragma with a #include
does not do the trick! Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 cabal 作为您的构建系统,并在
Library
的Extensions
字段或的
文件。然后从 Haskell 源文件中删除Executable
部分列出您想要的语言扩展project.cabalLANGUAGE
块。请参阅 Cabal 用户指南,包括简介的第三段。
Ghci 就是一切崩溃的地方。有人讨论添加一个
cabal ghci
命令,但同时它有点令人讨厌。如果您的项目是一个库,您可以运行 ghci -package-conf dist/package.conf.inplace 。
如果您想在 ghci 中加载未公开的模块,我会在您的
project.cabal
中定义一个“开发模式”标志:...有条件地在开发模式下公开额外的模块:
...并显式启用运行
cabal configure
时的开发模式:Use cabal as your build system, and list the language extensions you want in the
Extensions
field of theLibrary
orExecutable
section of yourproject.cabal
file. Then remove theLANGUAGE
block from your Haskell source files.See the Cabal User Guide, including the third paragraph of the introduction.
Ghci is where it all falls down. There is talk of adding a
cabal ghci
command, but in the meantime it's a bit icky.If your project is a library, you can run
ghci -package-conf dist/package.conf.inplace
.If you want to load unexposed modules in ghci, I'd define a "development mode" flag in your
project.cabal
:...conditionally expose extra modules in development mode:
...and explicitly enable development mode when you run
cabal configure
: