![](/img/trans.png)
[英]What's the difference between module, package and library in Haskell?
[英]On a stack's package file, what is the difference between library and executables dependencies?
在Haskell中,使用stack时,我们可以在三个地方定义对package.yaml
文件的依赖关系; 在library
和executables
下。 在这些地方定义依赖项和何时使用它们有什么区别?
假设您要使用两个前端构建和应用程序:CLI 和 GUI,两者都使用相同的底层逻辑。 显然,您的 CLI 不使用任何图形库,因此它不应该依赖它们,反之亦然。 你的package.yaml
看起来像这样。
name: my-app
version: 0.1.0.0
# This are the dependencies all the components depends on.
dependencies:
- base >= 4.7 && < 5
# This is the description of your "common logic" i.e. the library
library:
source-dirs: src
# depends on whatever libraries of your choice
dependencies:
- bytestring
- megaparsec
- parser-combinators
- transformers
executables:
# The executable name for the cli
my-app-cli:
# The main function is in cli.hs
main: cli.hs
source-dirs: app
ghc-options:
- -Wall
- -Wextra
- -threaded
- -rtsopts
- -with-rtsopts=-qg
# It depends on you library, the base library (which remember is at the top of the file), and on whatever other libraries you use to build the cli, for example optparse-applicative
dependencies:
- my-app
- optparse-applicative
# The executable name for the GUI
my-app-gui:
# The main function is on gui.hs
main: gui.hs
source-dirs: app
ghc-options:
- -Wall
- -Wextra
- -threaded
- -rtsopts
- -with-rtsopts=-qg
# Again, It depends on your library, the base, and a GUI library of your choice. Example monomer.
dependencies:
- my-app
- monomer
现在,当您运行stack build
时,它将创建两个可执行文件,一个名为my-app-cli
,另一个名为 my- my-app-gui
,每个都有自己的依赖项,但共享公共依赖项。 理想情况下,可以运行stack build my-app:my-app-cli
来构建其中一个可执行文件,但由于某种原因, stack
构建了所有内容(这显然是由于一些cabal
的行为......不知道,不要关心)
话虽如此,我认为这与其他编程语言没有什么不同。 例如,我倾向于以相同的方式构建我的Python
代码。 一个具有自己的requirements.txt
的公共库,然后是不同的应用程序,每个应用程序都有自己的requirement.txt
,具体取决于应用程序是 web 服务器、机器学习 model 还是其他... . 如果我需要向 web 服务器添加一些依赖项,则将它们添加到 web 服务器requirements.txt
而不是库之一。 这当然是固执己见,但我的观点是这不是 haskell 特定的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.