Skip to content

Commit d758b6c

Browse files
committed
Test cases
1 parent 0d5d572 commit d758b6c

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

tests/neg/opaque.scala

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+

tests/pos/opaque.scala

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
}

0 commit comments

Comments
 (0)