Skip to content

Commit 824c8ed

Browse files
author
Oron Port
committed
Update singleton-ops-covariance.scala
1 parent 9f36146 commit 824c8ed

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

tests/pos/singleton-ops-covariance.scala

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@ import scala.language.implicitConversions
55
object twoface {
66
protected object std {
77
type Int = scala.Int
8+
type Boolean = scala.Boolean
89
type String = java.lang.String
910
}
1011
export TwoFace.Int.extensions._
12+
export TwoFace.Boolean.extensions._
1113
object TwoFace {
1214
opaque type Int[T <: std.Int] = std.Int
1315
object Int {
1416
//constructors
15-
protected def create[T <: std.Int](value : std.Int) : Int[T] = value
17+
protected[TwoFace] def create[T <: std.Int](value : std.Int) : Int[T] = value
1618
def apply[T <: std.Int & Singleton](value : T) : Int[T] = create[T](value)
1719
//conversions
1820
given fromValue[T <: std.Int & Singleton] as Conversion[T, Int[T]] = t => create[T](t)
@@ -22,9 +24,32 @@ object twoface {
2224
given toWideTwoFace[T <: std.Int] as Conversion[Int[T], Int[std.Int]] = t => create[std.Int](t)
2325
//operations
2426
object extensions {
25-
def [L <: std.Int, R <: std.Int](left : Int[L]) + (right : Int[R]) : Int[int.+[L, R]] = create[int.+[L, R]](left + right)
27+
def [L <: std.Int, R <: std.Int](left : Int[L]) + (right : Int[R]) : Int[int.+[L, R]] = Int.create[int.+[L, R]](left + right)
28+
def [L <: std.Int, R <: std.Int](left : Int[L]) - (right : Int[R]) : Int[int.-[L, R]] = Int.create[int.-[L, R]](left - right)
29+
def [L <: std.Int, R <: std.Int](left : Int[L]) === (right : Int[R]) : Boolean[any.==[L, R]] = Boolean.create[any.==[L, R]](left == right)
2630
}
2731
}
32+
33+
opaque type Boolean[T <: std.Boolean] = std.Boolean
34+
object Boolean {
35+
//constructors
36+
protected[TwoFace] def create[T <: std.Boolean](value : std.Boolean) : Boolean[T] = value
37+
def apply[T <: std.Boolean & Singleton](value : T) : Boolean[T] = create[T](value)
38+
//conversions
39+
given fromValue[T <: std.Boolean & Singleton] as Conversion[T, Boolean[T]] = t => create[T](t)
40+
given fromWideValue(using DummyImplicit) as Conversion[std.Boolean, Boolean[std.Boolean]] = t => create[std.Boolean](t)
41+
given toValue[T <: std.Boolean](using ValueOf[T]) as Conversion[Boolean[T], T] = _ => valueOf[T]
42+
given toWideValue[T <: std.Boolean] as Conversion[Boolean[T], std.Boolean] = _.asInstanceOf[std.Boolean]
43+
given toWideTwoFace[T <: std.Boolean] as Conversion[Boolean[T], Boolean[std.Boolean]] = t => create[std.Boolean](t)
44+
//operations
45+
object extensions {
46+
def [L <: std.Boolean, R <: std.Boolean](left : Boolean[L]) === (right : Boolean[R]) : Boolean[any.==[L, R]] = Boolean.create[any.==[L, R]](left == right)
47+
}
48+
}
49+
}
50+
51+
object Checked {
52+
opaque type Int[T <: std.Int] = std.Int
2853
}
2954
}
3055

@@ -34,6 +59,8 @@ val oneCT = TwoFace.Int(1)
3459
val one : Int = 1
3560
val oneRT : TwoFace.Int[Int] = TwoFace.Int(one)
3661

62+
val b : TwoFace.Boolean[true] = oneCT === oneCT
63+
3764
val twoCT : TwoFace.Int[2] = oneCT + oneCT
3865
val a = TwoFace.Int(1) + 1
3966
val twoCT2 : TwoFace.Int[2] = a
@@ -52,6 +79,7 @@ val tfOne : TwoFace.Int[Int] = fromInt(one)
5279
object Test {
5380
class Vec[S <: Int](val size : TwoFace.Int[S]) {
5481
@infix def concat [RS <: Int](that : Vec[RS]) = new Vec(this.size + that.size)
82+
// @infix def == [RS <: Int](that : Vec[RS]) = this.size === that.size
5583
}
5684

5785
val one : Int = 1
@@ -60,7 +88,9 @@ object Test {
6088
val v3 : Vec[3] = v1 concat v2
6189
val v3a = v1 concat v2
6290
val v3b : Vec[3] = v3a
91+
// val b1 : true = v1 === v1
6392

6493
val vOne : Vec[Int] = new Vec(one)
6594
val vecs = for(i <- 1 to 3) yield (new Vec(i) concat v1)
6695
}
96+

0 commit comments

Comments
 (0)