Adding default package imports

前端 未结 4 1561
走了就别回头了
走了就别回头了 2021-02-09 07:03

In Java, Scala, or generally any JVM language, there is a set of packages that is imported by default. Java, for instance, automatically imports java.lang, you don\

相关标签:
4条回答
  • 2021-02-09 07:11

    Scala, as of 2.8, has package objects, the contents of which are automatically imported into every file of the package. Create a file called package.scala in the top level directory of your package (e.g., x/y/z/mypackage), and put into it

    package x.y.z
    
    package object mypackage {
      ...
    }
    

    Then any definition within the package object is automatically imported (i.e., no import statement is needed) into any file in package x.y.z.mypackage

    You can take a look at the Scala sources -- do a find dir -name package.scala -- there are a bunch of them, the topmost defining the implicit imports for the scala package itself, which are automatically imported into every scala source file.

    0 讨论(0)
  • 2021-02-09 07:19

    In Java there is not a way to do that.

    0 讨论(0)
  • 2021-02-09 07:28

    Jim Balter mentions in his answer (go upvote it) the Scala 2.8 package object.

    Any kind of definition that you can put inside a class, you can also put at the top level of a package. If you have some helper method you'd like to be in scope for an entire package, go ahead and put it right at the top level of the package.

    To do so, put the definitions in a package object. Each package is allowed to have one package object

    Scala actually has its own scala package object.
    See "Where to put package objects".

    So package object can solve the problem of explicit import for a given package, but not for any package.
    Its current limitations include:

    • First, you cannot define or inherit overloaded methods in package objects.
    • Second, you cannot define or inherit a member in a package object which is also the name of a top-level class or object in same package.

    We expect that some future Scala release will drop these restrictions.


    Original answer:

    On the Scala side, a question like "should Scala import scala.collection.JavaConversions._ by default?" shows that you cannot just add default imports as a Scala user.
    It has to be supported by the language.

    By the way, the conclusion was:

    From a Scala expert perspective: Let's keep things simple and limit the implicits that are imported by default to the absolute minimum.
    Heiko Seeberger


    Raphael does comment though:

    I guess you could write your own compiler plugin to add some packages?

    And sure enough, this Scala article describe how to extend the Scala compiler.
    But the main additions supported are:

    • You can add a phase to the compiler, thus adding extra checks or extra tree rewrites that apply after type checking has finished.
    • You can tell the compiler type-checking information about an annotation that is intended to be applied to types.

    So even with a tree rewrite (including additional imports), I am not sure said rewrite would be done in time for the types and functions (referenced by the implicit imports you are asking for) can be correctly analyzed.
    But maybe the Scala Compiler Corner has more ideas on that.

    0 讨论(0)
  • 2021-02-09 07:29

    The default import of all classes from java.lang is specified in the Java language Specification (7.5.5)

    All other classes have to be import with the import statement.

    0 讨论(0)
提交回复
热议问题