Monday, 21 December 2015

Package Object

The standard Scala package also has its package object. Because scala._ is automatically imported into every Scala file, the definitions of this object are available without prefix.
As you can see, the main purpose of this object is to make a number of often-used definitions nested in subpackages available from the scala package. 

For instance, the List type is used so often that it makes sense to put it in the scala package thereby making it accessible without an import or name qualification. For example,

  package object Errors {

    case class InvalidConfigurationException(val msg: String) extends RuntimeException(msg)
    case class UnsupportedFormatException(val msg: String) extends RuntimeException(msg)

}



Reference:

No comments:

Post a Comment