Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow new import syntax #11244

Merged
merged 10 commits into from Feb 8, 2021
Merged

Allow new import syntax #11244

merged 10 commits into from Feb 8, 2021

Conversation

odersky
Copy link
Contributor

@odersky odersky commented Jan 29, 2021

  • Replace wildcard import _ with *, which is what basically all other languages use
  • Replace renaming => with a soft keyword as.
  • Allow as outside braces, as in
    import scala.collection.mutable as mut
    import NumPy as np

From the doc page:

The syntax of wildcard and renaming imports (and exports) has changed.

Wildcard Imports

Wildcard imports are now expressed with * instead of underscore. Example:

import scala.annotation.*  // imports everything in the annotation package

If you want to import a member named * specifically, you can use backticks around it.

object A:
   def * = ...
   def min = ...

object B:
  import A.`*`   // imports just `*`

object C:
  import A.*     // imports everything in A

Renaming Imports

To rename or exclude an import, we now use as instead of =>. A single renaming import no longer needs to be enclosed in braces. Examples:

import A.{min as minimum, `*` as multiply}
import Predef.{augmentString as _, *}     // imports everything except augmentString
import scala.annotation as ann
import java as j

Migration

To support cross-building, Scala 3.0 supports the old import syntax with _ for wildcards and => for renamings in addition to the new one. The old syntax
will be dropped in a future versions. Automatic rewritings from old to new syntax
are offered under settings -source 3.1-migration -rewrite.

Syntax

Import            ::=  ‘import’ ImportExpr {‘,’ ImportExpr}
Export            ::=  ‘export’ ImportExpr {‘,’ ImportExpr}
ImportExpr        ::=  SimpleRef {‘.’ id} ‘.’ ImportSpec
ImportSpec        ::=  NamedSelector
                    |  WildcardSelector
                    | ‘{’ ImportSelectors) ‘}’
NamedSelector     ::=  id [‘as’ (id | ‘_’)]
WildCardSelector  ::=  ‘*' | ‘given’ [InfixType]
ImportSelectors   ::=  NamedSelector [‘,’ ImportSelectors]
                    |  WildCardSelector {‘,’ WildCardSelector}

@odersky odersky force-pushed the change-imports branch 2 times, most recently from 4df21b0 to 46fcfd6 Compare January 30, 2021 13:04
@odersky
Copy link
Contributor Author

odersky commented Jan 30, 2021

Based on #11240

@odersky odersky marked this pull request as ready for review January 30, 2021 17:14
@odersky odersky marked this pull request as draft January 30, 2021 17:28
@odersky odersky marked this pull request as ready for review January 31, 2021 10:49
 - Replace wildcard import `_` with `*`, which is what basically all other languages use
 - Replace renaming `=>` with a soft keyword `as`.
 - Allow `as` outside braces, as in
     ```scala
     import scala.collection.mutable as mut
     import NumPy as np
     ```
Support imports

    import a as b

where `a` is a simple identifier.
More test cases and fixes for simple renaming imports. Disallow
them for exports for now, since the primary use case of a renaming
export is not handled yet: lampepfl/dotty-feature-requests#148
@odersky
Copy link
Contributor Author

odersky commented Feb 8, 2021

@liufengyun Can you review the technical side here? It looks like the proposal has strong support, so we will probably go ahead and merge this.

Copy link
Contributor

@liufengyun liufengyun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

compiler/src/dotty/tools/dotc/parsing/Parsers.scala Outdated Show resolved Hide resolved
compiler/src/dotty/tools/dotc/parsing/Parsers.scala Outdated Show resolved Hide resolved
Co-authored-by: Fengyun Liu <liu@fengy.me>
@liufengyun liufengyun added this to the 3.0.0-RC1 milestone Feb 8, 2021
@odersky odersky merged commit ed5ecac into scala:master Feb 8, 2021
@odersky odersky deleted the change-imports branch February 8, 2021 17:15
@anatoliykmetyuk anatoliykmetyuk added the release-notes Should be mentioned in the release notes label Feb 10, 2021
@Kordyjan Kordyjan modified the milestones: 3.0.0-RC1, 3.0.0 Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-notes Should be mentioned in the release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants