Skip to content

Commit ba5ee26

Browse files
committed
Replace Logarithm example with Duration example
1 parent 0ad1ee4 commit ba5ee26

File tree

1 file changed

+27
-30
lines changed

1 file changed

+27
-30
lines changed

docs/blog/_posts/2018-11-27-11th-dotty-milestone-release.md

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -35,57 +35,54 @@ This is our 11th scheduled release according to our
3535
Opaque types aliases provide type abstraction without any overhead. Example:
3636

3737
```scala
38-
opaque type Logarithm = Double
38+
opaque type Duration = Long
3939
```
4040

41-
This introduces `Logarithm` as a new type, which is implemented as `Double` but is different from
42-
it. The fact that `Logarithm` is the same as `Double` is only known in the companion object of
43-
`Logarithm`. Here is a possible companion object:
41+
This introduces `Duration` as a new type, which is implemented as a `Long` but is different from
42+
it. The fact that `Duration` is the same as `Long` is only known in the companion object of
43+
`Duration`. Here is a possible companion object:
4444

4545
```scala
46-
object Logarithm {
46+
object Duration {
4747

48-
// These are the ways to lift to the logarithm type
49-
def apply(d: Double): Logarithm = math.log(d)
48+
// These are the ways to lift to the Duration type
49+
def fromNanos(duration: Long): Duration = duration
50+
def fromSeconds(duration: Long): Duration = duration * 1000000000
5051

51-
def safe(d: Double): Option[Logarithm] =
52-
if (d > 0.0) Some(math.log(d)) else None
53-
54-
// This is the first way to unlift the logarithm type
55-
def exponent(l: Logarithm): Double = l
52+
// This is the first way to unlift the Duration type
53+
def toNanos(l: Duration): Long = l
5654

5755
// Extension methods define opaque types' public APIs
58-
implicit class LogarithmOps(val `this`: Logarithm) extends AnyVal {
59-
// This is the second way to unlift the logarithm type
60-
def toDouble: Double = math.exp(`this`)
61-
def +(that: Logarithm): Logarithm = Logarithm(math.exp(`this`) + math.exp(that))
62-
def *(that: Logarithm): Logarithm = Logarithm(`this` + that)
56+
implicit class DurationOps(self: Duration) extends AnyVal {
57+
// This is the second way to unlift the Duration type
58+
def toSeconds: Long = self / 1000000000
59+
def + (that: Duration): Duration = self + that
6360
}
6461
}
6562
```
6663

67-
The companion object contains with the `apply` and `safe` methods ways to convert from doubles to
68-
`Logarithm` values. It also adds an `exponent` function and a decorator that implements `+` and `*`
69-
on logarithm values, as well as a conversion `toDouble`. All this is possible because within object
70-
`Logarithm`, the type `Logarithm` is just an alias of `Double`.
64+
The companion object contains the `fromNanos` and `fromSeconds` methods that convert from longs to
65+
`Duration` values. It also adds a `toNanos` function and a decorator that implements `+` on
66+
duration values, as well as a conversion `toSeconds`. All of this is possible because within object
67+
`Duration`, the type `Duration` is just an alias of `Long`.
7168

72-
Outside the companion object, `Logarithm` is treated as a new abstract type. So the following
73-
operations would be valid because they use functionality implemented in the `Logarithm`
69+
Outside the companion object, `Duration` is treated as a new abstract type. So the following
70+
operations would be valid because they use functionality implemented in the `Duration`
7471
object.
7572

7673
```scala
77-
val l = Logarithm(1.0)
78-
val l3 = l * l2
79-
val l4 = l + l2
74+
val d1 = Duration.fromNanos(1000L)
75+
val d2 = Duration.fromSeconds(2L)
76+
val d3 = d1 + d2
8077
```
8178

8279
But the following operations would lead to type errors:
8380

8481
```scala
85-
val d: Double = l // error: found: Logarithm, required: Double
86-
val l2: Logarithm = 1.0 // error: found: Double, required: Logarithm
87-
l * 2 // error: found: Int(2), required: Logarithm
88-
l / l2 // error: `/` is not a member fo Logarithm
82+
val l: Long = d1 // error: found: Duration, required: Long
83+
val d: Duration = 3L // error: found: Long(3L), required: Duration
84+
d1 + 2L // error: found: Long(2L), required: Duration
85+
d1 - d2 // error: `-` is not a member of Duration
8986
```
9087

9188
### Worksheet Mode Support in Visual Studio Code

0 commit comments

Comments
 (0)