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
2. The "magnet" pattern is sometimes used to express many variants of a method. Instead of defining overloaded versions of the method, one can also let the method take one or more arguments of specially defined "magnet" types, into which various argument types can be converted. Example:
46
-
```scala
47
-
objectCompletions {
46
+
```scala
47
+
objectCompletions {
48
48
49
-
// The argument "magnet" type
50
-
enumCompletionArg {
51
-
caseError(s: String)
52
-
caseResponse(f: Future[HttpResponse])
53
-
caseStatus(code: Future[StatusCode])
54
-
}
55
-
objectCompletionArg {
49
+
// The argument "magnet" type
50
+
enumCompletionArg {
51
+
caseError(s: String)
52
+
caseResponse(f: Future[HttpResponse])
53
+
caseStatus(code: Future[StatusCode])
54
+
}
55
+
objectCompletionArg {
56
56
57
-
// conversions defining the possible arguments to pass to `complete`
58
-
// these always come with CompletionArg
59
-
// They can be invoked explicitly, e.g.
60
-
//
61
-
// CompletionArg.fromStatusCode(statusCode)
57
+
// conversions defining the possible arguments to pass to `complete`
This setup is more complicated than simple overloading of `complete`, but it can still be useful if normal overloading is not available (as in the case above, since we cannot have two overloaded methods that take `Future[...]` arguments), or if normal overloading would lead to a combinatorial explosion of variants.
Copy file name to clipboardExpand all lines: docs/docs/reference/contextual/relationship-implicits.md
+4-4Lines changed: 4 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
---
2
2
layout: doc-page
3
-
title: Relationship with Scala 2 Implicits
3
+
title: "Relationship with Scala 2 Implicits"
4
4
---
5
5
6
6
Many, but not all, of the new contextual abstraction features in Scala 3 can be mapped to Scala 2's implicits. This page gives a rundown on the relationships between new and old features.
@@ -11,7 +11,7 @@ Many, but not all, of the new contextual abstraction features in Scala 3 can be
11
11
12
12
Given instances can be mapped to combinations of implicit objects, classes and implicit methods.
13
13
14
-
1. Given instances without parameters are mapped to implicit objects. E.g.,
14
+
1. Given instances without parameters are mapped to implicit objects. For instance,
15
15
16
16
```scala
17
17
givenintOrd:Ord[Int] with { ... }
@@ -23,7 +23,7 @@ Given instances can be mapped to combinations of implicit objects, classes and i
23
23
implicitobjectintOrdextendsOrd[Int] { ... }
24
24
```
25
25
26
-
2. Parameterized givens are mapped to combinations of classes and implicit methods. E.g.,
26
+
2. Parameterized givens are mapped to combinations of classes and implicit methods. For instance,
27
27
28
28
```scala
29
29
givenlistOrd[T](usingord: Ord[T]):Ord[List[T]] with { ... }
@@ -70,7 +70,7 @@ The synthesized type names are formed from
70
70
71
71
1. the prefix `given_`,
72
72
2. the simple name(s) of the implemented type(s), leaving out any prefixes,
73
-
3. the simple name(s) of the toplevel argument type constructors to these types.
73
+
3. the simple name(s) of the top-level argument type constructors to these types.
74
74
75
75
Tuples are treated as transparent, i.e. a type `F[(X, Y)]` would get the synthesized name
76
76
`F_X_Y`. Directly implemented function types `A => B` are represented as `A_to_B`. Function types used as arguments to other type constructors are represented as `Function`.
Copy file name to clipboardExpand all lines: docs/docs/reference/dropped-features/package-objects.md
+7-7Lines changed: 7 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -25,23 +25,23 @@ implicit object Cops {
25
25
extension (x: C) defpair(y: C) = (x, y)
26
26
}
27
27
```
28
-
There may be several source files in a package containing such toplevel definitions, and source files can freely mix toplevel value, method, and type definitions with classes and objects.
28
+
There may be several source files in a package containing such top-level definitions, and source files can freely mix top-level value, method, and type definitions with classes and objects.
29
29
30
-
The compiler generates synthetic objects that wrap toplevel definitions falling into one of the following categories:
30
+
The compiler generates synthetic objects that wrap top-level definitions falling into one of the following categories:
31
31
32
32
- all pattern, value, method, and type definitions,
33
33
- implicit classes and objects,
34
34
- companion objects of opaque type aliases.
35
35
36
-
If a source file `src.scala` contains such toplevel definitions, they will be put in a synthetic object named `src$package`. The wrapping is transparent, however. The definitions in `src` can still be accessed as members of the enclosing package.
36
+
If a source file `src.scala` contains such top-level definitions, they will be put in a synthetic object named `src$package`. The wrapping is transparent, however. The definitions in `src` can still be accessed as members of the enclosing package.
37
37
38
-
**Note 1:** This means that the name of a source file containing wrapped toplevel definitions is relevant for binary compatibility. If the name changes, so does the name of the generated object and its class.
38
+
**Note 1:** This means that the name of a source file containing wrapped top-level definitions is relevant for binary compatibility. If the name changes, so does the name of the generated object and its class.
39
39
40
-
**Note 2:** A toplevel main method `def main(args: Array[String]): Unit = ...` is wrapped as any other method. If it appears
40
+
**Note 2:** A top-level main method `def main(args: Array[String]): Unit = ...` is wrapped as any other method. If it appears
41
41
in a source file `src.scala`, it could be invoked from the command line using a command like `scala src$package`. Since the
42
42
"program name" is mangled it is recommended to always put `main` methods in explicitly named objects.
43
43
44
-
**Note 3:** The notion of `private` is independent of whether a definition is wrapped or not. A `private`toplevel definition is always visible from everywhere in the enclosing package.
44
+
**Note 3:** The notion of `private` is independent of whether a definition is wrapped or not. A `private`top-level definition is always visible from everywhere in the enclosing package.
45
45
46
-
**Note 4:** If several toplevel definitions are overloaded variants with the same name,
46
+
**Note 4:** If several top-level definitions are overloaded variants with the same name,
Copy file name to clipboardExpand all lines: docs/docs/reference/other-new-features/export.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -99,8 +99,8 @@ It is a standard recommendation to prefer composition over inheritance. This is
99
99
So far, object oriented languages including Scala made it much easier to use inheritance than composition. Inheritance only requires an `extends` clause whereas composition required a verbose elaboration of a sequence of forwarders. So in that sense, OO languages are pushing
100
100
programmers to a solution that is often too powerful. Export clauses redress the balance. They make composition relationships as concise and easy to express as inheritance relationships. Export clauses also offer more flexibility than extends clauses since members can be renamed or omitted.
101
101
102
-
Export clauses also fill a gap opened by the shift from package objects to toplevel definitions. One occasionally useful idiom that gets lost in this shift is a package object inheriting from some class. The idiom is often used in a facade like pattern, to make members
103
-
of internal compositions available to users of a package. Toplevel definitions are not wrapped in a user-defined object, so they can't inherit anything. However, toplevel definitions can be export clauses, which supports the facade design pattern in a safer and
102
+
Export clauses also fill a gap opened by the shift from package objects to top-level definitions. One occasionally useful idiom that gets lost in this shift is a package object inheriting from some class. The idiom is often used in a facade like pattern, to make members
103
+
of internal compositions available to users of a package. Top-level definitions are not wrapped in a user-defined object, so they can't inherit anything. However, top-level definitions can be export clauses, which supports the facade design pattern in a safer and
0 commit comments