Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit fb85502

Browse files
committedFeb 25, 2017
New packages and imports tour
1 parent 6b9bd6e commit fb85502

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed
 
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Packages and Imports
2+
Scala uses packages to create namespaces which allow you to modularize programs.
3+
4+
## Creating a package
5+
Packages are created by declaring one or more package names at the top of a Scala file.
6+
7+
```
8+
package users
9+
10+
class User
11+
```
12+
One convention is to name the package the same as the directory containing the Scala file. The directory structure of an sbt project for `package users` might look like this:
13+
```
14+
- ExampleProject
15+
- build.sbt
16+
- project
17+
- src
18+
- main
19+
- scala
20+
- users
21+
User.scala
22+
UserProfile.scala
23+
UserPreferences.scala
24+
- test
25+
```
26+
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:
27+
```
28+
package users {
29+
package administrators {
30+
class NormalUser
31+
}
32+
package normalusers {
33+
class NormalUser
34+
}
35+
}
36+
```
37+
As you can see, this allows for package nesting and provides greater control for scope and encapsulation.
38+
39+
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:
40+
```
41+
package com.google.selfdrivingcar.camera
42+
43+
class Lens
44+
```
45+
This could correspond to the following directory structure: `SelfDrivingCar/src/main/scala/com/google/selfdrivingcar/camera/Lens.scala`.
46+
47+
## Imports
48+
`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:
49+
```
50+
import users._ // import everything from the users package
51+
import users.User // import the class User
52+
import users.{User, UserPreferences} // Only imports selected members
53+
import users.{UserPreferences => UPrefs} // import and rename for convenience
54+
```
55+
56+
One way in which Scala is different from Java is that imports can be used anywhere:
57+
58+
```tut
59+
def sqrtplus1(x: Int) = {
60+
import scala.math.sqrt
61+
sqrt(x) + 1.0
62+
}
63+
```
64+
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__`:
65+
```
66+
package accounts
67+
68+
import __root__.users._
69+
```
70+
71+
72+
Note: The `scala` and `java.lang` packages as well as `object Predef` are imported by default.

0 commit comments

Comments
 (0)
Please sign in to comment.