Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
82 lines (58 sloc) 4.1 KB
This proposal was discussed at this pull request.

Allow qualified to follow module name

.. proposal-number:: 49
.. ticket-url:: https://gitlab.haskell.org/ghc/ghc/merge_requests/853
.. implemented::

We propose to allow the syntax import M qualified to solve hanging indents in module import lists.

49   Motivation

To import a qualified module you must specify qualified in prepositive position : import qualified M. This often leads to a "hanging indent" (which is automatically inserted by some autoformatters and common in many code bases). For example:

import qualified A
import           B
import           C

We propose to also allow qualified to appear in postpositive position : import M qualified. In that case, one could write:

import A qualified
import B
import C

While a small change, this annoyance is a significant issue in many large codebases, where programmers are forced to "pick a style" - chosing to add redundant whitespace to have modules align, or to make scanning import lists harder. Additionally, the location of qualified makes sorting imports harder.

50   Proposed Change Specification

A new language extension ImportQualifiedPost is introduced. When enabled, qualified will be accepted in postpositive position, by updating the importdecl production like so:

importdecl :: { LImportDecl GhcPs }
     : 'import' maybe_src maybe_safe optqualified maybe_pkg modid optqualified maybeas maybeimpspec

A new warning -Wprepositive-qualified-module (off by default) will be introduced that warns on prepositive syntax:

Preposition.hs:5:8: warning: [-Wprepositive-qualified-module]
    Found qualified in prepositive position
    Suggested fix: place  qualified after the module name instead.
  |
5 | import qualified Data.List
  |        ^^^^^^^^^

51   Effect and Interactions

The proposed change adds the ability to specify a qualified import by placing qualified either before or after the module name. Under ImportQualifiedPost, qualified will be permitted in either pre- or post-positive positions (but not both). If in the future we decide to deprecate the pre-positive syntax, we would make an extension flag for that and allow it to be turned off.

The position of the qualified does not change the semantics, and is independent from any other import features (e.g. package imports or safe annotations). As an example, both import qualified D as E and import D qualified as E are permitted and equivalent.

There should be no other interactions with any existing language or compiler features.

52   Costs and Drawbacks

The implementation of the change is but a few lines (Parser.y for the grammar and RdrHsSyn.hs for warnings/errors). The increased flexibility comes with no discernible drawbacks.

53   Alternatives

The alternatives appear to be: (1) Keep the status-quo and do not allow the alternate syntax; (2) Mandate the alternative syntax and formulate a migration strategy.

The second alternative solves the motivating hanging indent issue but in our opinion both alternatives seem needlessly strict when both conventions can be had cheaply with only upside.

54   Unresolved Questions

There are no remaining unresolved questions.

55   Implementation Plan

If accepted, the proposal authors will implement the change.

You can’t perform that action at this time.