Marking primitive types with phantom types in Scala

前端 未结 2 624
梦谈多话
梦谈多话 2021-02-10 04:31

In Scala I can use the concept of phantom types (as described e.g. here) to mark types and have this information erased at runtime. I wonder whether it is possible to mark primi

2条回答
  •  猫巷女王i
    2021-02-10 05:13

    The following works for me:

    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 */
    

提交回复
热议问题