@@ -35,57 +35,54 @@ This is our 11th scheduled release according to our
35
35
Opaque types aliases provide type abstraction without any overhead. Example:
36
36
37
37
``` scala
38
- opaque type Logarithm = Double
38
+ opaque type Duration = Long
39
39
```
40
40
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:
44
44
45
45
``` scala
46
- object Logarithm {
46
+ object Duration {
47
47
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
50
51
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
56
54
57
55
// 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
63
60
}
64
61
}
65
62
```
66
63
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 ` .
71
68
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 `
74
71
object.
75
72
76
73
``` 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
80
77
```
81
78
82
79
But the following operations would lead to type errors:
83
80
84
81
``` 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
89
86
```
90
87
91
88
### Worksheet Mode Support in Visual Studio Code
0 commit comments