Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell language extensions: pragma vs. compiler flag?

To use a language extension such as UnicodeSyntax in all files of a project, 2 options are:

  • A pragma in every source file: {-# LANGUAGE UnicodeSyntax #-}
  • Adding extensions: UnicodeSyntax in every section (library, executable, test) of the .cabal file.

Are there any reasons or best practices to choose one over the other?

I'm using stack and their standard new-template for simple packages.

like image 461
Jakob Bager Avatar asked Nov 18 '15 00:11

Jakob Bager


People also ask

What is Pragma Haskell?

The LANGUAGE pragma 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. The LANGUAGE pragma should be used instead of OPTIONS_GHC , if possible.

What are language extensions in Haskell?

Language extensions are used to enable language features in Haskell that may seem useful in certain cases. They can be used to loosen restrictions in the type system or add completely new language constructs to Haskell. or (in GHC) using flags -X<Extension> .

How do I enable my Haskell extension?

You can enable an extension for a single file by placing {-# LANGUAGE ExtensionName #-} on its own line at or near the top of the file (anywhere before the module header, or before the first import or definition if there is no module header, should be fine).


1 Answers

Personally, I always list all the language extensions in each file that uses them. Then you can tell exactly what extensions a particular piece of code is using, just by looking at the code. That means I can instantly tell, for example, is this code doing something with Template Haskell or not. And I don't have to look at any related files to figure that out. It also means I can compile just that module, manually from the command line, without having to memorise which extensions I need.

I suppose there is something to be said for being able to glance at the Cabal spec to see which extensions a particular package uses.

like image 53
MathematicalOrchid Avatar answered Nov 15 '22 09:11

MathematicalOrchid