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
Copy file name to clipboardExpand all lines: docs/docs/reference/other-new-features/opaques.md
+18-16Lines changed: 18 additions & 16 deletions
Original file line number
Diff line number
Diff line change
@@ -12,7 +12,8 @@ object Logarithms {
12
12
13
13
objectLogarithm {
14
14
15
-
// These are the ways to lift to the logarithm type
15
+
// These are the two ways to lift to the Logarithm type
16
+
16
17
defapply(d: Double):Logarithm= math.log(d)
17
18
18
19
defsafe(d: Double):Option[Logarithm] =
@@ -28,19 +29,22 @@ object Logarithms {
28
29
}
29
30
```
30
31
31
-
This introduces `Logarithm` as a new type, which is implemented as `Double` but is different from it. The fact that `Logarithm` is the same as `Double` is only known in the scope where
32
-
`Logarithm` is defined which in this case is object `Logarithms`.
33
-
34
-
The public API of `Logarithm` consists of the `apply` and `safe` methods that convert from doubles to `Logarithm` values, an extension method `toDouble` that converts the other way,
35
-
and operations `+` and `*` on logarithm values. The implementations of these functions
36
-
type-check because within object `Logarithms`, the type `Logarithm` is just an alias of `Double`.
32
+
This introduces `Logarithm` as a new abstract type, which is implemented as `Double`.
33
+
The fact that `Logarithm` is the same as `Double` is only known in the scope where
34
+
`Logarithm` is defined which in the above example corresponds to the object `Logarithms`.
35
+
Or in other words, within the scope it is treated as type alias, but this is opaque to the outside world
36
+
where in consequence `Logarithm` is treated as own type and has nothing to do with `Double`.
37
37
38
-
Outside its scope, `Logarithm` is treated as a new abstract type. So the
39
-
following operations would be valid because they use functionality implemented in the `Logarithm` object.
38
+
The public API of `Logarithm` consists of the `apply` and `safe` methods defined in the companion object.
39
+
They convert from `Double`s to `Logarithm` values. Moreover, a collective extension `logarithmOps` provides the extension methods `toDouble` that converts the other way,
40
+
and operations `+` and `*` on `Logarithm` values.
41
+
As a reminder, the implementations of these functions
42
+
type-check because within the object `Logarithms`, the type `Logarithm` is just a type alias of `Double`.
43
+
Outside of its scope, `Logarithm` is treated as a new abstract type. So the
44
+
following operations would be valid because they use functionality implemented in the `Logarithms` object.
40
45
41
46
```scala
42
-
importLogarithms._
43
-
importPredef.{any2stringadd=>_, _}
47
+
importLogarithms.Logarithm
44
48
45
49
vall=Logarithm(1.0)
46
50
vall2=Logarithm(2.0)
@@ -54,11 +58,9 @@ But the following operations would lead to type errors:
54
58
vald:Double= l // error: found: Logarithm, required: Double
Aside: the `any2stringadd => _` import suppression is necessary since otherwise the universal `+` operation in `Predef` would take precedence over the `+` extension method in `logarithmOps`. We plan to resolve this wart by eliminating `any2stringadd`.
61
-
62
64
### Bounds For Opaque Type Aliases
63
65
64
66
Opaque type aliases can also come with bounds. Example:
@@ -81,7 +83,7 @@ object Access {
81
83
valReadOrWrite:PermissionChoice=Read|Write
82
84
}
83
85
```
84
-
The `Access` object defines three opaque types:
86
+
The `Access` object defines three opaque type aliases:
85
87
86
88
-`Permission`, representing a single permission,
87
89
-`Permissions`, representing a set of permissions with the meaning "all of these permissions granted",
@@ -98,7 +100,7 @@ Because of that, the `|` extension method in `Access` does not cause infinite re
98
100
Also, the definition of `ReadWrite` must use `|`,
99
101
even though an equivalent definition outside `Access` would use `&`.
100
102
101
-
All three opaque types have the same underlying representation type `Int`. The
103
+
All three opaque type aliases have the same underlying representation type `Int`. The
102
104
`Permission` type has an upper bound `Permissions & PermissionChoice`. This makes
103
105
it known outside the `Access` object that `Permission` is a subtype of the other
104
106
two types. Hence, the following usage scenario type-checks.
0 commit comments