You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add more guidelines for labelling code examples for Scala 2/3 (#2768)
* Clarify how to label entire pages of documentation that are specific to a Scala version
* Add a note that all the code examples assume a specific version of Scala in the “version-specific-notice”
* Use the new way of labelling pages in Multiversal Equality
* Use the new way of labelling pages in Given Instances and Using Clauses, and adjust the “version-specific-notice”.
* Label “types-union”
* Also label the chinese version of types-union.md
* Label the page types-adts-gadts.md as “New in Scala 3”
Scala 3 offers two important feature for contextual abstraction:
15
14
16
15
-**Using Clauses** allow you to specify parameters that, at the call site, can be omitted by the programmer and should be automatically provided by the context.
@@ -23,9 +22,6 @@ One common way to achieve this is by passing the configuration as additional arg
23
22
24
23
In the following example, we define a case class `Config` to model some website configuration and pass it around in the different methods.
25
24
26
-
{% tabs nonusing %}
27
-
{% tab 'Scala 2 and 3' %}
28
-
29
25
```scala
30
26
caseclassConfig(port: Int, baseUrl: String)
31
27
@@ -38,19 +34,13 @@ val config = Config(8080, "docs.scala-lang.org")
38
34
renderWebsite("/home", config)
39
35
```
40
36
41
-
{% endtab %}
42
-
{% endtabs %}
43
-
44
37
Let us assume that the configuration does not change throughout most of our code base.
45
38
Passing `c` 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 `c` argument.
46
39
47
40
#### Using `using` to mark parameters as contextual
48
41
49
42
In Scala 3, we can mark some parameters of our methods as _contextual_.
By starting a parameter section with the keyword `using`, we tell the Scala compiler that at the call-site it should automatically find an argument with the correct type.
67
54
The Scala compiler thus performs **term inference**.
68
55
@@ -71,36 +58,24 @@ So the program is equivalent to the one above.
71
58
72
59
In fact, since we do not need to refer to `c` in our implementation of `renderWebsite` anymore, we can even omit its name in the signature:
We have seen how to _abstract_ over contextual parameters and that the Scala compiler can provide arguments automatically for us.
90
71
But how can we specify which configuration to use for our call to `renderWebsite`?
91
72
92
73
Like we specified our parameter section with `using`, we can also explicitly provide contextual arguments with `using:`
93
74
94
-
{% tabs using3 %}
95
-
{% tab 'Scala 3 Only' %}
96
-
97
75
```scala
98
76
renderWebsite("/home")(using config)
99
77
```
100
78
101
-
{% endtab %}
102
-
{% endtabs %}
103
-
104
79
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.
105
80
106
81
For all other cases, as we will see in the next Section, there is also another way to bring contextual values into scope.
@@ -110,9 +85,6 @@ For all other cases, as we will see in the next Section, there is also another w
110
85
We have seen that we can explicitly pass arguments as contextual parameters by marking the argument section of the _call_ with `using`.
111
86
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`.
112
87
113
-
{% tabs given1 %}
114
-
{% tab 'Scala 3 Only' %}
115
-
116
88
```scala
117
89
valconfig=Config(8080, "docs.scala-lang.org")
118
90
// this is the type that we want to provide the
@@ -124,24 +96,15 @@ given Config = config
124
96
// as argument to contextual parameters of type Config
125
97
```
126
98
127
-
{% endtab %}
128
-
{% endtabs %}
129
-
130
99
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.
131
100
132
101
Having defined a given for `Config`, we can simply call `renderWebsite`:
Copy file name to clipboardExpand all lines: _overviews/scala3-book/ca-multiversal-equality.md
+2-5
Original file line number
Diff line number
Diff line change
@@ -6,12 +6,9 @@ languages: [zh-cn]
6
6
num: 64
7
7
previous-page: ca-type-classes
8
8
next-page: ca-implicit-conversions
9
+
scala3: true
10
+
versionSpecific: true
9
11
---
10
-
<spanclass="tag tag-inline">New In Scala 3</span>
11
-
12
-
> Multiversal Equality is a new language feature that was introduced in Scala 3.
13
-
> Because it has no equivalent in Scala 2, all code examples
14
-
> in this lesson assume you are using Scala 3.
15
12
16
13
Previously, Scala had *universal equality*: Two values of any types could be compared with each other using `==` and `!=`.
17
14
This came from the fact that `==` and `!=` are implemented in terms of Java’s `equals` method, which can also compare values of any two reference types.
Copy file name to clipboardExpand all lines: _overviews/scala3-book/types-union.md
+4-1
Original file line number
Diff line number
Diff line change
@@ -6,6 +6,8 @@ languages: [zh-cn]
6
6
num: 51
7
7
previous-page: types-intersection
8
8
next-page: types-adts-gadts
9
+
scala3: true
10
+
versionSpecific: true
9
11
---
10
12
11
13
Used on types, the `|` operator creates a so-called _union type_.
@@ -43,14 +45,15 @@ case 1.0 => ??? // ERROR: this line won’t compile
43
45
As shown, union types can be used to represent alternatives of several different types, without requiring those types to be part of a custom-crafted class hierarchy, or requiring explicit wrapping.
44
46
45
47
#### Pre-planning the Class Hierarchy
46
-
Other languages would require pre-planning of the class hierarchy, like the following example illustrates:
48
+
Without union types, it would require pre-planning of the class hierarchy, like the following example illustrates:
0 commit comments