如何在没有长长的 CPP if/else 条件列表的情况下在代码中获取 haskell 编译器版本?

发布于 2025-01-18 10:49:20 字数 424 浏览 5 评论 0原文

按照以下路线的内容:

import GHC.Version qualified

main :: IO ()
main = print $ GHC.Version.current
-- would print Version 8 10 7

回旋方式是从Shell中执行GHC - Version

import System.Process

main = system "ghc --version"

但是,当运行GHC与GHC不相同时,这可能是不正确的。在路径中。

Something along the lines of:

import GHC.Version qualified

main :: IO ()
main = print $ GHC.Version.current
-- would print Version 8 10 7

A roundabout way is to execute ghc --version from shell:

import System.Process

main = system "ghc --version"

but this may be incorrect when the running GHC isn't the same as the ghc in PATH.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

源来凯始玺欢你 2025-01-25 10:49:20

是的,有两种方式。第一个使用 System.Info.compilerVersion< /code>

import System.Info

main :: IO ()
main = print compilerVersion
-- prints "Version {versionBranch = [8,6], versionTags = []}"

第二个通过 CPP 语言扩展:

{-# LANGUAGE CPP #-}

main :: IO ()
main = print __GLASGOW_HASKELL__
-- prints "806" (of type Integer)

文档说明该数字如何已经想出来了。

Yes, in two ways. The first using System.Info.compilerVersion

import System.Info

main :: IO ()
main = print compilerVersion
-- prints "Version {versionBranch = [8,6], versionTags = []}"

Second via the CPP language extension:

{-# LANGUAGE CPP #-}

main :: IO ()
main = print __GLASGOW_HASKELL__
-- prints "806" (of type Integer)

Docs on how that number has been come up with.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文