-
Notifications
You must be signed in to change notification settings - Fork 1k
Updated pages to be “Scala 3 Only” #2689
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
+51
−20
Merged
Changes from all commits
Commits
Show all changes
3 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
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 |
---|---|---|
@@ -1,50 +1,60 @@ | ||
--- | ||
title: Context Bounds | ||
type: section | ||
description: This page demonstrates Context Bounds in Scala 3. | ||
description: This page demonstrates Context Bounds in Scala. | ||
languages: [zh-cn] | ||
num: 61 | ||
previous-page: ca-given-using-clauses | ||
next-page: ca-given-imports | ||
--- | ||
|
||
|
||
{% comment %} | ||
- TODO: define "context parameter" | ||
- TODO: define "synthesized" and "synthesized arguments" | ||
{% endcomment %} | ||
|
||
In many situations the name of a _context parameter_ doesn’t have to be mentioned explicitly, since it’s only used by the compiler in synthesized arguments for other context parameters. | ||
In many situations the name of a [context parameter]({% link _overviews/scala3-book/ca-given-using-clauses.md %}#using-clauses) doesn’t have to be mentioned explicitly, since it’s only used by the compiler in synthesized arguments for other context parameters. | ||
In that case you don’t have to define a parameter name, and can just provide the parameter type. | ||
|
||
|
||
## Background | ||
|
||
For example, this `maximum` method takes a _context parameter_ of type `Ord`, only to pass it on as an argument to `max`: | ||
|
||
{% tabs context-bounds-max-named-param class=tabs-scala-version %} | ||
|
||
{% tab 'Scala 2' %} | ||
```scala | ||
def maximum[A](xs: List[A])(using ord: Ord[A]): A = | ||
def maximum[A](xs: List[A])(implicit ord: Ord[A]): A = | ||
xs.reduceLeft(max(ord)) | ||
``` | ||
{% endtab %} | ||
|
||
In that code the parameter name `ord` isn’t actually required; it can be passed on as an inferred argument to `max`, so you just state that `maximum` uses the type `Ord[A]` without giving it a name: | ||
|
||
{% tab 'Scala 3' %} | ||
```scala | ||
def maximum[A](xs: List[A])(using Ord[A]): A = | ||
xs.reduceLeft(max) | ||
def maximum[A](xs: List[A])(using ord: Ord[A]): A = | ||
xs.reduceLeft(max(using ord)) | ||
``` | ||
{% endtab %} | ||
|
||
{% endtabs %} | ||
|
||
## Context bounds | ||
|
||
Given that background, a _context bound_ is a shorthand syntax for expressing the pattern of, “a context parameter that depends on a type parameter.” | ||
Given that background, a _context bound_ is a shorthand syntax for expressing the pattern of, “a context parameter applied to a type parameter.” | ||
|
||
Using a context bound, the `maximum` method can be written like this: | ||
|
||
{% tabs context-bounds-max-rewritten %} | ||
|
||
{% tab 'Scala 2 and 3' %} | ||
|
||
```scala | ||
def maximum[A: Ord](xs: List[A]): A = xs.reduceLeft(max) | ||
def maximum[A: Ord](xs: List[A]): A = | ||
xs.reduceLeft(max) | ||
``` | ||
|
||
A bound like `: Ord` on a type parameter `A` of a method or class indicates a context parameter with `Ord[A]`. | ||
{% endtab %} | ||
|
||
{% endtabs %} | ||
|
||
|
||
A bound like `: Ord` on a type parameter `A` of a method or class indicates a context parameter with type `Ord[A]`. | ||
Under the hood, the compiler transforms this syntax into the one shown in the Background section. | ||
|
||
For more information about context bounds, see the [“What are context bounds?”](https://docs.scala-lang.org/tutorials/FAQ/context-bounds.html) section of the Scala FAQ. | ||
For more information about context bounds, see the [“What are context bounds?”]({% link _overviews/FAQ/index.md %}#what-are-context-bounds) section of the Scala FAQ. |
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
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
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
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.