-
Notifications
You must be signed in to change notification settings - Fork 1k
Add code tabs for _tour/packages-and-imports #2570
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
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: `<top-level-domain>.<domain-name>.<project-name>`. 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 | ||
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._ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should have a scala 3 tab for |
||
``` | ||
|
||
{% endtab %} | ||
{% endtabs %} | ||
|
||
Note: The `scala` and `java.lang` packages as well as `object Predef` are imported by default. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.