Skip to content

Commit 1ebb5e1

Browse files
flomebulbishabosha
andauthored
Add code tabs for _tour/packages-and-imports (#2570)
* Add code tabs for _tour/packages-and-imports * Add code tabs for _tour/packages-and-imports * Update _tour/packages-and-imports.md Co-authored-by: Jamie Thompson <bishbashboshjt@gmail.com> * Add code tabs for _tour/packages-and-imports Co-authored-by: Jamie Thompson <bishbashboshjt@gmail.com>
1 parent b22aa06 commit 1ebb5e1

File tree

1 file changed

+66
-3
lines changed

1 file changed

+66
-3
lines changed

_tour/packages-and-imports.md

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,16 @@ Scala uses packages to create namespaces which allow you to modularize programs.
1414
## Creating a package
1515
Packages are created by declaring one or more package names at the top of a Scala file.
1616

17+
{% tabs packages-and-imports_1 %}
18+
{% tab 'Scala 2 and 3' for=packages-and-imports_1 %}
1719
```
1820
package users
1921
2022
class User
2123
```
24+
{% endtab %}
25+
{% endtabs %}
26+
2227
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:
2328
```
2429
- ExampleProject
@@ -34,7 +39,10 @@ One convention is to name the package the same as the directory containing the S
3439
- test
3540
```
3641
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:
37-
```
42+
43+
{% tabs packages-and-imports_2 class=tabs-scala-version %}
44+
{% tab 'Scala 2' for=packages-and-imports_2 %}
45+
```scala
3846
package users {
3947
package administrators {
4048
class NormalUser
@@ -44,39 +52,94 @@ package users {
4452
}
4553
}
4654
```
55+
{% endtab %}
56+
{% tab 'Scala 3' for=packages-and-imports_2 %}
57+
```scala
58+
package users:
59+
package administrators:
60+
class NormalUser
61+
package normalusers:
62+
class NormalUser
63+
```
64+
{% endtab %}
65+
{% endtabs %}
66+
4767
As you can see, this allows for package nesting and provides greater control for scope and encapsulation.
4868

4969
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: `<top-level-domain>.<domain-name>.<project-name>`. For example, if Google had a project called `SelfDrivingCar`, the package name would look like this:
50-
```
70+
71+
{% tabs packages-and-imports_3 %}
72+
{% tab 'Scala 2 and 3' for=packages-and-imports_3 %}
73+
```scala
5174
package com.google.selfdrivingcar.camera
5275

5376
class Lens
5477
```
78+
{% endtab %}
79+
{% endtabs %}
80+
5581
This could correspond to the following directory structure: `SelfDrivingCar/src/main/scala/com/google/selfdrivingcar/camera/Lens.scala`.
5682

5783
## Imports
5884
`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:
85+
86+
{% tabs packages-and-imports_4 class=tabs-scala-version %}
87+
{% tab 'Scala 2' for=packages-and-imports_4 %}
5988
```
6089
import users._ // import everything from the users package
6190
import users.User // import the class User
6291
import users.{User, UserPreferences} // Only imports selected members
6392
import users.{UserPreferences => UPrefs} // import and rename for convenience
6493
```
94+
{% endtab %}
95+
{% tab 'Scala 3' for=packages-and-imports_4 %}
96+
```
97+
import users.* // import everything from the users package except given
98+
import users.given // import all given from the users package
99+
import users.User // import the class User
100+
import users.{User, UserPreferences} // Only imports selected members
101+
import users.UserPreferences as UPrefs // import and rename for convenience
102+
```
103+
{% endtab %}
104+
{% endtabs %}
65105

66106
One way in which Scala is different from Java is that imports can be used anywhere:
67107

108+
{% tabs packages-and-imports_5 class=tabs-scala-version %}
109+
{% tab 'Scala 2' for=packages-and-imports_5 %}
68110
```scala mdoc
69111
def sqrtplus1(x: Int) = {
70112
import scala.math.sqrt
71113
sqrt(x) + 1.0
72114
}
73115
```
74-
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_`:
116+
{% endtab %}
117+
{% tab 'Scala 3' for=packages-and-imports_5 %}
118+
```scala
119+
def sqrtplus1(x: Int) =
120+
import scala.math.sqrt
121+
sqrt(x) + 1.0
75122
```
123+
{% endtab %}
124+
{% endtabs %}
125+
126+
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_`:
127+
128+
{% tabs packages-and-imports_6 class=tabs-scala-version %}
129+
{% tab 'Scala 2' for=packages-and-imports_6 %}
130+
```scala
76131
package accounts
77132

78133
import _root_.users._
79134
```
135+
{% endtab %}
136+
{% tab 'Scala 3' for=packages-and-imports_6 %}
137+
```scala
138+
package accounts
80139

140+
import _root_.users.*
141+
```
142+
{% endtab %}
143+
{% endtabs %}
81144

82145
Note: The `scala` and `java.lang` packages as well as `object Predef` are imported by default.

0 commit comments

Comments
 (0)