File tree 2 files changed +42
-0
lines changed
2 files changed +42
-0
lines changed Original file line number Diff line number Diff line change
1
+ object opaquetypes {
2
+ opaque val x : Int = 1 // error
3
+
4
+ opaque class Foo // error
5
+
6
+ opaque type T // error
7
+
8
+ opaque type U <: String // error
9
+
10
+ opaque type O = String
11
+
12
+ val s : O = " " // error
13
+
14
+ object O {
15
+ val s : O = " " // should be OK
16
+ }
17
+
18
+ }
19
+
Original file line number Diff line number Diff line change
1
+ object opaquetypes {
2
+ opaque type Logarithm = Double
3
+
4
+ object Logarithm {
5
+
6
+ // These are the ways to lift to the logarithm type
7
+ def apply (d : Double ): Logarithm = math.log(d)
8
+
9
+ def safe (d : Double ): Option [Logarithm ] =
10
+ if (d > 0.0 ) Some (math.log(d)) else None
11
+
12
+ // This is the first way to unlift the logarithm type
13
+ def exponent (l : Logarithm ): Double = l
14
+
15
+ // Extension methods define opaque types' public APIs
16
+ implicit class LogarithmOps (val `this` : Logarithm ) extends AnyVal {
17
+ // This is the second way to unlift the logarithm type
18
+ def toDouble : Double = math.exp(`this`)
19
+ def + (that : Logarithm ): Logarithm = Logarithm (math.exp(`this`) + math.exp(that))
20
+ def * (that : Logarithm ): Logarithm = Logarithm (`this` + that)
21
+ }
22
+ }
23
+ }
You can’t perform that action at this time.
0 commit comments