简体   繁体   English

Haskell pragmas:OPTIONS_GHC vs LANGUAGE

[英]Haskell pragmas: OPTIONS_GHC vs LANGUAGE

I find myself using this sort of pragma a lot in my cabal projects to force GHC to build with specific options: 我发现自己在我的阴谋项目中使用了这种类型的pragma来迫使GHC使用特定的选项进行构建:

{-# OPTIONS_GHC -XFlexibleInstances -XRankNTypes ... #-}

But when I see other people using extentions, they always declare it in this way: 但当我看到其他人使用扩展时,他们总是以这种方式声明它:

{-# LANGUAGE FlexibleInstances, RankNTypes, ... #-}

However, when I load files in GHCi which use the latter method, GHC always complains that I'm using an unrecognised pragma and promptly fails. 但是,当我在使用后一种方法的GHCi中加载文件时,GHC总是抱怨我使用了一个unrecognised pragma并立即失败。

Why does GHC not accept the LANGUAGE pragma, and which of the two is better practice? 为什么GHC不接受LANGUAGE编译指示,哪两个是更好的做法?


Note: my GHC version is up-to-date: 7.8.3, but was 7.6.* when this occurred. 注意:我的GHC版本是最新的:7.8.3,但是当发生这种情况时是7.6。*。

Using the LANGUAGE pragma is nicer. 使用LANGUAGE编译指示更好。 Instead of giving a list of arbitrary options it is specific only to language extensions. 它不是给出任意选项的列表,而是仅针对语言扩展。 It's also designed to be portable between implementations, as mentioned in the GHC docs; 如GHC文档中所述,它还可以在实现之间移植; LANGUAGE

…allows language extensions to be enabled in a portable way. ...允许以便携方式启用语言扩展。 It is the intention that all Haskell compilers support the LANGUAGE pragma with the same syntax, although not all extensions are supported by all compilers, of course. 所有Haskell编译器都支持使用相同语法的LANGUAGE编译指示,当然并非所有编译器都支持所有扩展。 The LANGUAGE pragma should be used instead of OPTIONS_GHC , if possible. 如果可能,应使用LANGUAGE编译指示而不是OPTIONS_GHC [Emphasis added] [强调补充]

This same language shows up in the docs all the way from GHC 6.6 (§7.10.4) , when LANGUAGE was introduced, up through GHC 7.10.1 (§7.22.1) , the current (at time of writing) release. 从引入LANGUAGE GHC 6.6(§7.10.4)到最新的GHC 7.10.1(§7.22.1) ,当前(撰写本文时)发布时,同样的语言出现在文档中。

A third way of specifying language extensions would be to declare them in the cabal file. 指定语言扩展的第三种方法是在cabal文件中声明它们。

I also find that it's common to use LANGUAGE pragma to declare used extensions for a single file, but using OPTIONS_GHC (on a level of single file) is usually used as a workaround (eg to disable some warnings). 我还发现使用LANGUAGE pragma为单个文件声明已使用的扩展是很常见的,但是使用OPTIONS_GHC (在单个文件的级别上)通常用作解决方法(例如,禁用某些警告)。 People prefer to declare GHC options project-wide (with cabal) not on individual files, whereas for some reason language extensions are often used on per-file basis. 人们更喜欢在项目范围内(使用cabal)声明GHC选项而不是单个文件,而由于某些原因,语言扩展通常用于每个文件。

Here are a few wild guesses why the LANGUAGE pragma might not be recognised: 以下是一些猜测可能无法识别LANGUAGE编译指示的猜测:

  • You have an ancient GHC version (< 6.6) 你有一个古老的GHC版本(<6.6)
  • You do not declare it as a file-header pragma. 您不会将其声明为文件头编译指示。 A file-header pragma must precede the module keyword in the file. 文件头编译指示必须位于文件中的module关键字之前。 In other words, it should be at the very top of the file (though it might be preceded by other file-header pragmas) 换句话说,它应该位于文件的最顶层(尽管它可能在其他文件头编译指示之前)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM