From fa11f8782149307a2e43a61b324b09cf6b8da6cb Mon Sep 17 00:00:00 2001 From: Seth Tisue Date: Tue, 4 Dec 2018 12:11:26 -0800 Subject: [PATCH 1/3] copyedit new package object tour section --- _tour/package-objects.md | 45 +++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/_tour/package-objects.md b/_tour/package-objects.md index f787e3cc0f..b0af960ed3 100644 --- a/_tour/package-objects.md +++ b/_tour/package-objects.md @@ -10,13 +10,16 @@ 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 +# Package objects + +Scala provides package objects as a convenient container shared across an entire package. + +Package objects +can contain arbitrary definitions, not just variable and method definitions. For instance, they are 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` +By convention, the source code for a package object is usually put in a source file named `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. @@ -29,46 +32,46 @@ See example below. Assume first a class `Fruit` and three `Fruit` objects in a p 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") +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`. + +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) + val planted = List(Apple, Plum, Banana) + def showFruit(fruit: Fruit): Unit = { + println(s"${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 +As an example of how the use site looks, 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) { + def main(args: Array[String]): Unit = { + for (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 objects are like other objects, which means you can use inheritance for building them. For example, one might mix in a couple of traits: + ``` -package object fruits extends FruitAliases - with FruitHelpers { -// helpers and variables follows here +package object fruits extends FruitAliases with FruitHelpers { + // helpers and variables follows here } ``` -Note that method overloading doesn't work in package objects. +Note that method overloading doesn't work in package objects. From d10ffefee83803a5704b676123da55d18e17b7c4 Mon Sep 17 00:00:00 2001 From: Seth Tisue Date: Tue, 4 Dec 2018 13:24:43 -0800 Subject: [PATCH 2/3] supply placeholders for new tour section --- _ba/tour/package-objects.md | 17 +++++++++++++++++ _es/tour/package-objects.md | 17 +++++++++++++++++ _ko/tour/package-objects.md | 17 +++++++++++++++++ _pl/tour/package-objects.md | 17 +++++++++++++++++ _pt-br/tour/package-objects.md | 17 +++++++++++++++++ _th/tour/package-objects.md | 17 +++++++++++++++++ _zh-cn/tour/package-objects.md | 17 +++++++++++++++++ 7 files changed, 119 insertions(+) create mode 100644 _ba/tour/package-objects.md create mode 100644 _es/tour/package-objects.md create mode 100644 _ko/tour/package-objects.md create mode 100644 _pl/tour/package-objects.md create mode 100644 _pt-br/tour/package-objects.md create mode 100644 _th/tour/package-objects.md create mode 100644 _zh-cn/tour/package-objects.md diff --git a/_ba/tour/package-objects.md b/_ba/tour/package-objects.md new file mode 100644 index 0000000000..69976a661f --- /dev/null +++ b/_ba/tour/package-objects.md @@ -0,0 +1,17 @@ +--- +layout: tour +title: Package Objects +language: ba + +discourse: true + +partof: scala-tour + +num: 35 +previous-page: packages-and-imports +--- + +# Package objects + +(this section of the tour has not been translated yet. pull request +with translation welcome!) diff --git a/_es/tour/package-objects.md b/_es/tour/package-objects.md new file mode 100644 index 0000000000..41cf1540a0 --- /dev/null +++ b/_es/tour/package-objects.md @@ -0,0 +1,17 @@ +--- +layout: tour +title: Package Objects +language: es + +discourse: true + +partof: scala-tour + +num: 35 +previous-page: packages-and-imports +--- + +# Package objects + +(this section of the tour has not been translated yet. pull request +with translation welcome!) diff --git a/_ko/tour/package-objects.md b/_ko/tour/package-objects.md new file mode 100644 index 0000000000..9e18affc95 --- /dev/null +++ b/_ko/tour/package-objects.md @@ -0,0 +1,17 @@ +--- +layout: tour +title: Package Objects +language: ko + +discourse: true + +partof: scala-tour + +num: 35 +previous-page: packages-and-imports +--- + +# Package objects + +(this section of the tour has not been translated yet. pull request +with translation welcome!) diff --git a/_pl/tour/package-objects.md b/_pl/tour/package-objects.md new file mode 100644 index 0000000000..cbcfe3399d --- /dev/null +++ b/_pl/tour/package-objects.md @@ -0,0 +1,17 @@ +--- +layout: tour +title: Package Objects +language: pl + +discourse: true + +partof: scala-tour + +num: 35 +previous-page: packages-and-imports +--- + +# Package objects + +(this section of the tour has not been translated yet. pull request +with translation welcome!) diff --git a/_pt-br/tour/package-objects.md b/_pt-br/tour/package-objects.md new file mode 100644 index 0000000000..6c903fec68 --- /dev/null +++ b/_pt-br/tour/package-objects.md @@ -0,0 +1,17 @@ +--- +layout: tour +title: Package Objects +language: pt-br + +discourse: true + +partof: scala-tour + +num: 35 +previous-page: packages-and-imports +--- + +# Package objects + +(this section of the tour has not been translated yet. pull request +with translation welcome!) diff --git a/_th/tour/package-objects.md b/_th/tour/package-objects.md new file mode 100644 index 0000000000..645393b7d0 --- /dev/null +++ b/_th/tour/package-objects.md @@ -0,0 +1,17 @@ +--- +layout: tour +title: Package Objects +language: zh-cn + +discourse: true + +partof: scala-tour + +num: 35 +previous-page: packages-and-imports +--- + +# Package objects + +(this section of the tour has not been translated yet. pull request +with translation welcome!) diff --git a/_zh-cn/tour/package-objects.md b/_zh-cn/tour/package-objects.md new file mode 100644 index 0000000000..d1e455124d --- /dev/null +++ b/_zh-cn/tour/package-objects.md @@ -0,0 +1,17 @@ +--- +layout: tour +title: Package Objects +language: th + +discourse: true + +partof: scala-tour + +num: 35 +previous-page: packages-and-imports +--- + +# Package objects + +(this section of the tour has not been translated yet. pull request +with translation welcome!) From bca84a91cfd6ca01b6370ccdaf66f36922bf3577 Mon Sep 17 00:00:00 2001 From: Seth Tisue Date: Tue, 4 Dec 2018 13:29:00 -0800 Subject: [PATCH 3/3] fixups to links to new package objects tour section --- _ba/tour/package-objects.md | 2 +- _ba/tour/packages-and-imports.md | 2 +- _es/tour/package-objects.md | 2 +- _es/tour/packages-and-imports.md | 2 +- _ko/tour/package-objects.md | 2 +- _ko/tour/packages-and-imports.md | 1 + _pl/tour/package-objects.md | 2 +- _pl/tour/packages-and-imports.md | 2 +- _pt-br/tour/package-objects.md | 2 +- _pt-br/tour/packages-and-imports.md | 2 +- _th/tour/package-objects.md | 4 ++-- _th/tour/packages-and-imports.md | 1 + _tour/package-objects.md | 2 +- _tour/packages-and-imports.md | 1 + _zh-cn/tour/package-objects.md | 4 ++-- _zh-cn/tour/packages-and-imports.md | 1 + 16 files changed, 18 insertions(+), 14 deletions(-) diff --git a/_ba/tour/package-objects.md b/_ba/tour/package-objects.md index 69976a661f..e3a020e0f7 100644 --- a/_ba/tour/package-objects.md +++ b/_ba/tour/package-objects.md @@ -7,7 +7,7 @@ discourse: true partof: scala-tour -num: 35 +num: 36 previous-page: packages-and-imports --- diff --git a/_ba/tour/packages-and-imports.md b/_ba/tour/packages-and-imports.md index b466c5946a..dcc73e2d73 100644 --- a/_ba/tour/packages-and-imports.md +++ b/_ba/tour/packages-and-imports.md @@ -9,7 +9,7 @@ partof: scala-tour num: 35 previous-page: named-arguments -next-page: type-inference +next-page: package-objects --- # Packages and Imports diff --git a/_es/tour/package-objects.md b/_es/tour/package-objects.md index 41cf1540a0..600e428523 100644 --- a/_es/tour/package-objects.md +++ b/_es/tour/package-objects.md @@ -7,7 +7,7 @@ discourse: true partof: scala-tour -num: 35 +num: 36 previous-page: packages-and-imports --- diff --git a/_es/tour/packages-and-imports.md b/_es/tour/packages-and-imports.md index 6f1e51d951..3d1c4989d2 100644 --- a/_es/tour/packages-and-imports.md +++ b/_es/tour/packages-and-imports.md @@ -9,7 +9,7 @@ partof: scala-tour num: 35 previous-page: named-arguments -next-page: type-inference +next-page: package-objects --- # Packages and Imports diff --git a/_ko/tour/package-objects.md b/_ko/tour/package-objects.md index 9e18affc95..99e15aa8ef 100644 --- a/_ko/tour/package-objects.md +++ b/_ko/tour/package-objects.md @@ -7,7 +7,7 @@ discourse: true partof: scala-tour -num: 35 +num: 36 previous-page: packages-and-imports --- diff --git a/_ko/tour/packages-and-imports.md b/_ko/tour/packages-and-imports.md index b1a226f711..6b77e31189 100644 --- a/_ko/tour/packages-and-imports.md +++ b/_ko/tour/packages-and-imports.md @@ -10,6 +10,7 @@ num: 35 language: ko previous-page: named-arguments +next-page: package-objects --- # 패키지와 임포트 diff --git a/_pl/tour/package-objects.md b/_pl/tour/package-objects.md index cbcfe3399d..05b11f78c1 100644 --- a/_pl/tour/package-objects.md +++ b/_pl/tour/package-objects.md @@ -7,7 +7,7 @@ discourse: true partof: scala-tour -num: 35 +num: 36 previous-page: packages-and-imports --- diff --git a/_pl/tour/packages-and-imports.md b/_pl/tour/packages-and-imports.md index 724704030b..5d5b61c2af 100644 --- a/_pl/tour/packages-and-imports.md +++ b/_pl/tour/packages-and-imports.md @@ -9,7 +9,7 @@ partof: scala-tour num: 35 previous-page: named-arguments -next-page: type-inference +next-page: package-objects --- # Packages and Imports diff --git a/_pt-br/tour/package-objects.md b/_pt-br/tour/package-objects.md index 6c903fec68..09db5aee84 100644 --- a/_pt-br/tour/package-objects.md +++ b/_pt-br/tour/package-objects.md @@ -7,7 +7,7 @@ discourse: true partof: scala-tour -num: 35 +num: 36 previous-page: packages-and-imports --- diff --git a/_pt-br/tour/packages-and-imports.md b/_pt-br/tour/packages-and-imports.md index abc74a9f2a..797e58a2fd 100644 --- a/_pt-br/tour/packages-and-imports.md +++ b/_pt-br/tour/packages-and-imports.md @@ -9,7 +9,7 @@ partof: scala-tour num: 35 previous-page: named-arguments -next-page: type-inference +next-page: package-objects --- # Packages and Imports diff --git a/_th/tour/package-objects.md b/_th/tour/package-objects.md index 645393b7d0..337a18cf41 100644 --- a/_th/tour/package-objects.md +++ b/_th/tour/package-objects.md @@ -1,13 +1,13 @@ --- layout: tour title: Package Objects -language: zh-cn +language: th discourse: true partof: scala-tour -num: 35 +num: 36 previous-page: packages-and-imports --- diff --git a/_th/tour/packages-and-imports.md b/_th/tour/packages-and-imports.md index 4f6d714a25..da591b618a 100644 --- a/_th/tour/packages-and-imports.md +++ b/_th/tour/packages-and-imports.md @@ -11,4 +11,5 @@ num: 33 language: th previous-page: named-arguments +next-page: package-objects --- diff --git a/_tour/package-objects.md b/_tour/package-objects.md index b0af960ed3..9a59550b4a 100644 --- a/_tour/package-objects.md +++ b/_tour/package-objects.md @@ -6,7 +6,7 @@ discourse: true partof: scala-tour -num: 35 +num: 36 previous-page: packages-and-imports --- diff --git a/_tour/packages-and-imports.md b/_tour/packages-and-imports.md index 35c9b198b2..68efdd8083 100644 --- a/_tour/packages-and-imports.md +++ b/_tour/packages-and-imports.md @@ -8,6 +8,7 @@ partof: scala-tour num: 35 previous-page: named-arguments +next-page: package-objects --- # Packages and Imports diff --git a/_zh-cn/tour/package-objects.md b/_zh-cn/tour/package-objects.md index d1e455124d..ee95910149 100644 --- a/_zh-cn/tour/package-objects.md +++ b/_zh-cn/tour/package-objects.md @@ -1,13 +1,13 @@ --- layout: tour title: Package Objects -language: th +language: zh-cn discourse: true partof: scala-tour -num: 35 +num: 36 previous-page: packages-and-imports --- diff --git a/_zh-cn/tour/packages-and-imports.md b/_zh-cn/tour/packages-and-imports.md index a4a1713b1b..56f4de1371 100644 --- a/_zh-cn/tour/packages-and-imports.md +++ b/_zh-cn/tour/packages-and-imports.md @@ -11,6 +11,7 @@ num: 33 language: zh-cn previous-page: named-arguments +next-page: package-objects --- # 包和导入