From 14656bcb0b0ea4f9bd66f33878a428a7670a40a8 Mon Sep 17 00:00:00 2001 From: Zafar Khaydarov Date: Thu, 1 Nov 2018 10:58:40 -0400 Subject: [PATCH 1/2] add-package-objects-docs * sources: - https://www.scala-lang.org/docu/files/packageobjects/packageobjects.html - https://stackoverflow.com/questions/3400734/package-objects - https://github.com/scala/scala/blob/2.13.x/src/library/scala/package.scala --- _tour/package-ojects.md | 74 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 _tour/package-ojects.md diff --git a/_tour/package-ojects.md b/_tour/package-ojects.md new file mode 100644 index 0000000000..f787e3cc0f --- /dev/null +++ b/_tour/package-ojects.md @@ -0,0 +1,74 @@ +--- +layout: tour +title: Package Objects + +discourse: true + +partof: scala-tour + +num: 35 +previous-page: packages-and-imports +--- + +# Package Objects +Scala provides you package object as convenient container shared across the package. Package objects +can contain arbitrary definitions, not just variable and method definitions. For instance, they frequently +used to hold package-wide type aliases and implicit conversions. Package objects can even inherit +Scala classes and traits. + +Package objects usually created as separate scala file in the package lavel - `package.scala` + +Each package is allowed to have one package object. Any definitions placed in a package object are considered +members of the package itself. + +See example below. Assume first a class `Fruit` and three `Fruit` objects in a package +`gardening.fruits`: + +``` +// in file gardening/fruits/Fruit.scala +package gardening.fruits + +case class Fruit(name: String, color: String) +object apple extends Fruit("Apple", "green") +object plum extends Fruit("Plum", "blue") +object banana extends Fruit("Banana", "yellow") +``` +Now assume you want to place a variable planted and a method `showFruit` directly into package `gardening`. +Here's how this is done: +``` +// in file gardening/fruits/package.scala +package gardening +package object fruits { + val planted = List(apple, plum, banana) + def showFruit(fruit: Fruit) { + println(fruit.name +"s are "+ fruit.color) + } +} +``` + +Having the package Object above, any other code in the same package can import the method just like it would import +a class. For example, the following object `PrintPlanted` imports `planted` and `showFruit` in exactly the same +way it imports class `Fruit`, using a wildcard import on package gardening.fruits: + +``` +// in file PrintPlanted.scala +import gardening.fruits._ +object PrintPlanted { + def main(args: Array[String]) { + for (fruit: Fruit <- fruits.planted) { + showFruit(fruit) + } + } +} +``` +Having package object also helps reducing number of imports on client use. + +Package objects are usual objects. That means you can use inheritance for building them (for exmple from traits): +``` +package object fruits extends FruitAliases + with FruitHelpers { +// helpers and variables follows here +} +``` +Note that method overloading doesn't work in package objects. + From 0e5906019b9a0777bb9ba67cc27dddd9b94258d9 Mon Sep 17 00:00:00 2001 From: Zafar Khaydarov Date: Thu, 1 Nov 2018 11:21:19 -0400 Subject: [PATCH 2/2] Rename package-ojects.md to package-objects.md - fix typo in file name [apple keyboard is so ugly] --- _tour/{package-ojects.md => package-objects.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename _tour/{package-ojects.md => package-objects.md} (100%) diff --git a/_tour/package-ojects.md b/_tour/package-objects.md similarity index 100% rename from _tour/package-ojects.md rename to _tour/package-objects.md