scala - Package objects -


What are package objects, not too much concept but their use?

I have tried to get an example to work and the only form of work I had was as follows:

  package object test {val package object = value "package Object Values ​​"} Package Inquiry {Object Package Object Test {DIF Main: Array [String]) {println (" Package Object Valve Referencing: "+ PackageObjectVal)}}}  

Comments made up to:

  package object _root_ {...}  

is not allowed (which is appropriate),

  package object xy {...}  

is also not allowed .

It seems that the package object should be declared in the immediate parent package and if the above is written above, then the brace delimited package announcement form is required.

Are they in common use? if so, how?

Normally you will put your package object in a separate file named package .scala It is related to that package. You can also use nested package syntax but it is quite unusual.

The main use case for package objects is an example when you are inside your package as well as the API package:

  // file: foo / Bar / package.scala package foo package object bar {// package detailed constants: Def BarVersionString = "1.0" // Type of nickname type stringmap [+ T] = map [string, T] / use a package detailed import Can be used to simulate / especially when typing the Java API If time of the time is wrapped, then the timeout = org.joda.time.DateTime type JList [T] = java. Use .list [t] // defined definitions required to effectively use your API: vested AIBB (A: A): B = // ...}  

now The definitions package object is available in the entire package foo.bar . In addition, definitions are imported when a foo.bar ._ outside any package.

In this way you can prevent the need for the API Client to use its library to issue additional imports. Effectively - like writing a scale-swing you must

  import swing ._ import swing ._  

Like all code < Code> onEDT and Tuple2 to dimension .


Comments

Popular posts from this blog

Eclipse CDT variable colors in editor -

AJAX doesn't send POST query -

wpf - Custom Message Box Advice -