zig*_*tar 5 types static-analysis scala
在斯卡拉我可以用幻影类型的概念(如例如描述这里)来标记类型,并有在运行时删除该信息.我想知道是否可以用幻像类型标记原始类型而不用它们装箱.
一个例子可以是一个函数,它允许Int只在它是素数时才通过.签名可能类似于以下内容:
def filterPrime(i: Int): Option[Int with IsPrime]
Run Code Online (Sandbox Code Playgroud)
Some(i)如果i是素数,则函数返回值None.
在没有装箱原始整数的情况下,Scala可以实现所提出的想法吗?
以下适用于我:
trait IsPrime
val x = 5.asInstanceOf[Int with IsPrime]
val y:Int = x
val z:Int with IsPrime = 6 /* this line causes a compile error
which is what you want */
Run Code Online (Sandbox Code Playgroud)