Skip to content

Instantly share code, notes, and snippets.

@sir-wabbit
Created September 28, 2017 16:18
Show Gist options
  • Save sir-wabbit/f5717f08e2500ef52a1124df817ba794 to your computer and use it in GitHub Desktop.
Save sir-wabbit/f5717f08e2500ef52a1124df817ba794 to your computer and use it in GitHub Desktop.
import scala.reflect.ClassTag
object Test {
type Flags = Flags.Type
trait Flags1 {
import Flags._
type Type <: Int with Tag$$1
}
object Flags extends Flags1 {
trait Tag$$1 extends Any
object Impl {
def apply(value: Int): Type = value.asInstanceOf[Type]
def unwrap(value: Type): Int = value.asInstanceOf[Int]
def subst[F[_]](value: F[Int]): F[Type] = value.asInstanceOf[F[Type]]
}
implicit val equiv: ClassTag[Type] = Impl.subst[ClassTag](implicitly[ClassTag[Int]])
}
type Opaque = Opaque.Type
trait Opaque1 {
import Opaque._
type Base$$1
type Type <: Base$$1 with Tag$$1
}
object Opaque extends Opaque1 {
trait Tag$$1 extends Any
object Impl {
def apply(value: Int): Type = value.asInstanceOf[Type]
def unwrap(value: Type): Int = value.asInstanceOf[Int]
def subst[F[_]](value: F[Int]): F[Type] = value.asInstanceOf[F[Type]]
}
implicit val equiv: ClassTag[Type] = Impl.subst[ClassTag](implicitly[ClassTag[Int]])
}
def main(args: Array[String]): Unit = {
implicitly[Flags <:< Int]
println(implicitly[ClassTag[Flags]].newArray(10))
println(implicitly[ClassTag[Opaque]].newArray(10))
val arr: Array[Flags] = Flags.Impl.subst[Array]((Array(1): Array[Int]))
println(arr(0)) // 1
println(arr.getClass) // class [I, which is int[] !
val v: Flags = Flags.Impl.apply(1)
println(v) // 1
println(v.getClass) // java.lang.Integer
val arr1: Array[Opaque] = Opaque.Impl.subst[Array]((Array(1): Array[Int]))
println(arr1(0)) // 1
println(arr1.getClass) // class [I, which is int[] !
val v1: Opaque = Opaque.Impl.apply(1)
println(v1) // 1
println(v1.getClass) // java.lang.Integer
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment