Adding default package imports

前端 未结 4 1626
走了就别回头了
走了就别回头了 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.

提交回复
热议问题