Package objects

后端 未结 4 911
你的背包
你的背包 2020-12-02 07:03

What are package objects, not so much the concept but their usage?

I\'ve tried to get an example working and the only form I got to work was as follows:



        
4条回答
  •  攒了一身酷
    2020-12-02 07:38

    Normally you would put your package object in a separate file called package.scala in the package that it corresponds to. You can also use the nested package syntax but that is quite unusual.

    The main use case for package objects is when you need definitions in various places inside your package as well as outside the package when you use the API defined by the package. Here is an example:

    // file: foo/bar/package.scala
    
    package foo
    
    package object bar {
    
      // package wide constants:
      def BarVersionString = "1.0"
    
      // or type aliases
      type StringMap[+T] = Map[String,T]
    
      // can be used to emulate a package wide import
      // especially useful when wrapping a Java API
      type DateTime = org.joda.time.DateTime
    
      type JList[T] = java.util.List[T]
    
      // Define implicits needed to effectively use your API:
      implicit def a2b(a: A): B = // ...
    
    }
    

    Now the definitions inside that package object are available inside the whole package foo.bar. Furthermore the definitions get imported when someone outside of that package imports foo.bar._.

    This way you can prevent to require the API client to issue additional imports to use your library effectively - e.g. in scala-swing you need to write

    import swing._
    import Swing._
    

    to have all the goodness like onEDT and implicit conversions from Tuple2 to Dimension.

提交回复
热议问题