From 745591d21f180a5badd31cd9d4121e11f77044bf Mon Sep 17 00:00:00 2001 From: Luc Henninger Date: Wed, 28 Sep 2022 16:35:06 +0200 Subject: [PATCH 1/4] Add code tabs for _tour/packages-and-imports --- _tour/packages-and-imports.md | 62 +++++++++++++++++++++++++++++++++-- 1 file changed, 59 insertions(+), 3 deletions(-) diff --git a/_tour/packages-and-imports.md b/_tour/packages-and-imports.md index f18f8ae5e3..3a2a37566b 100644 --- a/_tour/packages-and-imports.md +++ b/_tour/packages-and-imports.md @@ -14,11 +14,16 @@ Scala uses packages to create namespaces which allow you to modularize programs. ## Creating a package Packages are created by declaring one or more package names at the top of a Scala file. +{% tabs packages-and-imports_1 %} +{% tab 'Scala 2 and 3' for=packages-and-imports_1 %} ``` package users class User ``` +{% endtab %} +{% endtabs %} + One convention is to name the package the same as the directory containing the Scala file. However, Scala is agnostic to file layout. The directory structure of an sbt project for `package users` might look like this: ``` - ExampleProject @@ -34,7 +39,10 @@ One convention is to name the package the same as the directory containing the S - test ``` Notice how the `users` directory is within the `scala` directory and how there are multiple Scala files within the package. Each Scala file in the package could have the same package declaration. The other way to declare packages is by using braces: -``` + +{% tabs packages-and-imports_2 class=tabs-scala-version %} +{% tab 'Scala 2' for=packages-and-imports_2 %} +```scala mdoc package users { package administrators { class NormalUser @@ -44,39 +52,87 @@ package users { } } ``` +{% endtab %} +{% tab 'Scala 3' for=packages-and-imports_2 %} +```scala +package users: + package administrators: + class NormalUser + package normalusers: + class NormalUser +``` +{% endtab %} +{% endtabs %} + As you can see, this allows for package nesting and provides greater control for scope and encapsulation. The package name should be all lower case and if the code is being developed within an organization which has a website, it should be the following format convention: `..`. For example, if Google had a project called `SelfDrivingCar`, the package name would look like this: -``` + +{% tabs packages-and-imports_3 %} +{% tab 'Scala 2 and 3' for=packages-and-imports_3 %} +```scala mdoc package com.google.selfdrivingcar.camera class Lens ``` +{% endtab %} +{% endtabs %} + This could correspond to the following directory structure: `SelfDrivingCar/src/main/scala/com/google/selfdrivingcar/camera/Lens.scala`. ## Imports `import` clauses are for accessing members (classes, traits, functions, etc.) in other packages. An `import` clause is not required for accessing members of the same package. Import clauses are selective: + +{% tabs packages-and-imports_4 class=tabs-scala-version %} +{% tab 'Scala 2' for=packages-and-imports_4 %} ``` import users._ // import everything from the users package import users.User // import the class User import users.{User, UserPreferences} // Only imports selected members import users.{UserPreferences => UPrefs} // import and rename for convenience ``` +{% endtab %} +{% tab 'Scala 3' for=packages-and-imports_4 %} +``` +import users.* // import everything from the users package except given +import users.given // import all given from the users package +import users.User // import the class User +import users.{User, UserPreferences} // Only imports selected members +import users.{UserPreferences => UPrefs} // import and rename for convenience +``` +{% endtab %} +{% endtabs %} One way in which Scala is different from Java is that imports can be used anywhere: +{% tabs packages-and-imports_5 class=tabs-scala-version %} +{% tab 'Scala 2' for=packages-and-imports_5 %} ```scala mdoc def sqrtplus1(x: Int) = { import scala.math.sqrt sqrt(x) + 1.0 } ``` +{% endtab %} +{% tab 'Scala 3' for=packages-and-imports_5 %} +```scala +def sqrtplus1(x: Int) = + import scala.math.sqrt + sqrt(x) + 1.0 +``` +{% endtab %} +{% endtabs %} + In the event there is a naming conflict and you need to import something from the root of the project, prefix the package name with `_root_`: + +{% tabs packages-and-imports_6 %} +{% tab 'Scala 2 and 3' for=packages-and-imports_6 %} ``` package accounts import _root_.users._ ``` - +{% endtab %} +{% endtabs %} Note: The `scala` and `java.lang` packages as well as `object Predef` are imported by default. From 8f22969a519aa532410d7efbbb1954adbbe64fc8 Mon Sep 17 00:00:00 2001 From: Luc Henninger Date: Wed, 28 Sep 2022 16:40:24 +0200 Subject: [PATCH 2/4] Add code tabs for _tour/packages-and-imports --- _tour/packages-and-imports.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/_tour/packages-and-imports.md b/_tour/packages-and-imports.md index 3a2a37566b..f5f47a9730 100644 --- a/_tour/packages-and-imports.md +++ b/_tour/packages-and-imports.md @@ -42,7 +42,7 @@ Notice how the `users` directory is within the `scala` directory and how there a {% tabs packages-and-imports_2 class=tabs-scala-version %} {% tab 'Scala 2' for=packages-and-imports_2 %} -```scala mdoc +```scala package users { package administrators { class NormalUser @@ -70,7 +70,7 @@ The package name should be all lower case and if the code is being developed wit {% tabs packages-and-imports_3 %} {% tab 'Scala 2 and 3' for=packages-and-imports_3 %} -```scala mdoc +```scala package com.google.selfdrivingcar.camera class Lens From 6d5f9b1d40afcf5887a840876f37c33e3802d147 Mon Sep 17 00:00:00 2001 From: Luc Henninger Date: Wed, 5 Oct 2022 11:39:46 +0200 Subject: [PATCH 3/4] Update _tour/packages-and-imports.md Co-authored-by: Jamie Thompson --- _tour/packages-and-imports.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_tour/packages-and-imports.md b/_tour/packages-and-imports.md index f5f47a9730..89b4cd6cc9 100644 --- a/_tour/packages-and-imports.md +++ b/_tour/packages-and-imports.md @@ -98,7 +98,7 @@ import users.* // import everything from the users package except given import users.given // import all given from the users package import users.User // import the class User import users.{User, UserPreferences} // Only imports selected members -import users.{UserPreferences => UPrefs} // import and rename for convenience +import users.UserPreferences as UPrefs // import and rename for convenience ``` {% endtab %} {% endtabs %} From ec977389592f5ac7e6b4c077d932565936953300 Mon Sep 17 00:00:00 2001 From: Luc Henninger Date: Wed, 5 Oct 2022 11:47:33 +0200 Subject: [PATCH 4/4] Add code tabs for _tour/packages-and-imports --- _tour/packages-and-imports.md | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/_tour/packages-and-imports.md b/_tour/packages-and-imports.md index 89b4cd6cc9..cd1ab1f6db 100644 --- a/_tour/packages-and-imports.md +++ b/_tour/packages-and-imports.md @@ -125,14 +125,21 @@ def sqrtplus1(x: Int) = In the event there is a naming conflict and you need to import something from the root of the project, prefix the package name with `_root_`: -{% tabs packages-and-imports_6 %} -{% tab 'Scala 2 and 3' for=packages-and-imports_6 %} -``` +{% tabs packages-and-imports_6 class=tabs-scala-version %} +{% tab 'Scala 2' for=packages-and-imports_6 %} +```scala package accounts import _root_.users._ ``` {% endtab %} +{% tab 'Scala 3' for=packages-and-imports_6 %} +```scala +package accounts + +import _root_.users.* +``` +{% endtab %} {% endtabs %} Note: The `scala` and `java.lang` packages as well as `object Predef` are imported by default.