-
Notifications
You must be signed in to change notification settings - Fork 1k
Add code tabs to Contextual Abstractions #2787
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 all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9cfb9e7
Add missing redirection from https://docs.scala-lang.org/scala3/book/…
julienrf a6fd020
Follow the standard way of labelling a page to be Scala 3 only
julienrf da3b344
Mark “Extension Methods” as Scala 3 specific, and add cross-reference…
julienrf 836e98b
Add Scala 2 tabs and explanations in the Implicit Conversions page
julienrf f23fa7d
Add code tabs to page 'Context Parameters'
julienrf 9702227
Add tabs to Type Classes page
julienrf 1b2bac1
Update intro
julienrf 92c1f63
Address feedback from review
julienrf 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
julienrf marked this conversation as resolved.
Show resolved
Hide resolved
|
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 |
---|---|---|
@@ -0,0 +1,157 @@ | ||
--- | ||
title: Context Parameters | ||
type: section | ||
description: This page demonstrates how to declare context parameters, and how the compiler infers them at call-site. | ||
languages: [zh-cn] | ||
num: 60 | ||
previous-page: ca-extension-methods | ||
next-page: ca-context-bounds | ||
redirect_from: /scala3/book/ca-given-using-clauses.html | ||
--- | ||
|
||
Scala offers two important features for contextual abstraction: | ||
|
||
- **Context Parameters** allow you to specify parameters that, at the call-site, can be omitted by the programmer and should be automatically provided by the context. | ||
- **Given Instances** (in Scala 3) or **Implicit Definitions** (in Scala 2) are terms that can be used by the Scala compiler to fill in the missing arguments. | ||
|
||
## Context Parameters | ||
|
||
When designing a system, often context information like _configuration_ or settings need to be provided to the different components of your system. | ||
One common way to achieve this is by passing the configuration as additional argument to your methods. | ||
|
||
In the following example, we define a case class `Config` to model some website configuration and pass it around in the different methods. | ||
|
||
{% tabs example %} | ||
{% tab 'Scala 2 and 3' %} | ||
```scala | ||
case class Config(port: Int, baseUrl: String) | ||
|
||
def renderWebsite(path: String, config: Config): String = | ||
"<html>" + renderWidget(List("cart"), config) + "</html>" | ||
|
||
def renderWidget(items: List[String], config: Config): String = ??? | ||
|
||
val config = Config(8080, "docs.scala-lang.org") | ||
renderWebsite("/home", config) | ||
``` | ||
{% endtab %} | ||
{% endtabs %} | ||
|
||
Let us assume that the configuration does not change throughout most of our code base. | ||
Passing `config` to each and every method call (like `renderWidget`) becomes very tedious and makes our program more difficult to read, since we need to ignore the `config` argument. | ||
|
||
### Marking parameters as contextual | ||
|
||
We can mark some parameters of our methods as _contextual_. | ||
|
||
{% tabs 'contextual-parameters' class=tabs-scala-version %} | ||
{% tab 'Scala 2' %} | ||
```scala | ||
def renderWebsite(path: String)(implicit config: Config): String = | ||
"<html>" + renderWidget(List("cart")) + "</html>" | ||
// ^ | ||
// no argument config required anymore | ||
|
||
def renderWidget(items: List[String])(implicit config: Config): String = ??? | ||
``` | ||
{% endtab %} | ||
{% tab 'Scala 3' %} | ||
```scala | ||
def renderWebsite(path: String)(using config: Config): String = | ||
"<html>" + renderWidget(List("cart")) + "</html>" | ||
// ^ | ||
// no argument config required anymore | ||
|
||
def renderWidget(items: List[String])(using config: Config): String = ??? | ||
``` | ||
{% endtab %} | ||
{% endtabs %} | ||
|
||
By starting a parameter section with the keyword `using` in Scala 3 or `implicit` in Scala 2, we tell the compiler that at the call-site it should automatically find an argument with the correct type. | ||
The Scala compiler thus performs **term inference**. | ||
|
||
In our call to `renderWidget(List("cart"))` the Scala compiler will see that there is a term of type `Config` in scope (the `config`) and automatically provide it to `renderWidget`. | ||
So the program is equivalent to the one above. | ||
|
||
In fact, since we do not need to refer to `config` in our implementation of `renderWebsite` anymore, we can even omit its name in the signature in Scala 3: | ||
|
||
{% tabs 'anonymous' %} | ||
{% tab 'Scala 3 Only' %} | ||
```scala | ||
// no need to come up with a parameter name | ||
// vvvvvvvvvvvvv | ||
def renderWebsite(path: String)(using Config): String = | ||
"<html>" + renderWidget(List("cart")) + "</html>" | ||
``` | ||
{% endtab %} | ||
{% endtabs %} | ||
|
||
In Scala 2, the name of implicit parameters is still mandatory. | ||
|
||
### Explicitly providing contextual arguments | ||
|
||
We have seen how to _abstract_ over contextual parameters and that the Scala compiler can provide arguments automatically for us. | ||
But how can we specify which configuration to use for our call to `renderWebsite`? | ||
|
||
{% tabs 'explicit' class=tabs-scala-version %} | ||
{% tab 'Scala 2' %} | ||
We explicitly supply the argument value as if it was a regular argument: | ||
```scala | ||
renderWebsite("/home")(config) | ||
``` | ||
{% endtab %} | ||
{% tab 'Scala 3' %} | ||
Like we specified our parameter section with `using`, we can also explicitly provide contextual arguments with `using`: | ||
```scala | ||
renderWebsite("/home")(using config) | ||
``` | ||
{% endtab %} | ||
{% endtabs %} | ||
|
||
Explicitly providing contextual parameters can be useful if we have multiple different values in scope that would make sense, and we want to make sure that the correct one is passed to the function. | ||
|
||
For all other cases, as we will see in the next section, there is also another way to bring contextual values into scope. | ||
|
||
## Given Instances (Implicit Definitions in Scala 2) | ||
|
||
We have seen that we can explicitly pass arguments as contextual parameters. | ||
However, if there is _a single canonical value_ for a particular type, there is another preferred way to make it available to the Scala compiler: by marking it as `given` in Scala 3 or `implicit` in Scala 2. | ||
|
||
{% tabs 'instances' class=tabs-scala-version %} | ||
{% tab 'Scala 2' %} | ||
```scala | ||
implicit val config: Config = Config(8080, "docs.scala-lang.org") | ||
// ^^^^^^ | ||
// this is the value the Scala compiler will infer | ||
// as argument to contextual parameters of type Config | ||
``` | ||
{% endtab %} | ||
{% tab 'Scala 3' %} | ||
```scala | ||
val config = Config(8080, "docs.scala-lang.org") | ||
julienrf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// this is the type that we want to provide the | ||
// canonical value for | ||
// vvvvvv | ||
given Config = config | ||
// ^^^^^^ | ||
// this is the value the Scala compiler will infer | ||
// as argument to contextual parameters of type Config | ||
``` | ||
{% endtab %} | ||
{% endtabs %} | ||
|
||
In the above example we specify that whenever a contextual parameter of type `Config` is omitted in the current scope, the compiler should infer `config` as an argument. | ||
|
||
Having defined a canonical value for the type `Config`, we can call `renderWebsite` as follows: | ||
|
||
```scala | ||
renderWebsite("/home") | ||
// ^ | ||
// again no argument | ||
``` | ||
|
||
A detailed guide to where Scala looks for canonical values can be found in [the FAQ]({% link _overviews/FAQ/index.md %}#where-does-scala-look-for-implicits). | ||
|
||
[reference]: {{ site.scala3ref }}/overview.html | ||
[blog-post]: /2020/11/06/explicit-term-inference-in-scala-3.html |
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
Oops, something went wrong.
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.