我是scala和精化库的新手,但我尝试创建基于UUID的两种精化类型。
为了做到这一点,我这样做了(注意:在本例中,Uuid来自eu.timepit.refined.string.Uuid):
type UuidPredicate = Uuid
type UuidA = String Refined UuidPredicate
type UuidB = String Refined UuidPredicate然而,似乎这只是创建别名,因此没有类型安全。
因此,如果我有一个像Product(a UuidA, b UuidB)这样的构造函数,然后继续这样做:
val myUuid: UuidA = "9f9ef0c6-b6f8-11ea-b3de-0242ac130004"
val Product = Product(myUuid, myUuid)它将正确编译和运行。是否要确保情况不是这样呢?如果变量是作为一种类型创建的,那么如何才能使它仅作为特定的精化类型使用,即使类型基本上是相同的呢?
发布于 2020-06-25 18:02:21
最简单的方法是引入不同的数据类型。
case class UuidA(value: String Refined UuidPredicate)
case class UuidB(value: String Refined UuidPredicate)您不能使UuidA,UuidB扩展AnyVal,因为Refined已经扩展了AnyVal,Scala不允许嵌套的值类。
如果您希望避免使用UuidA包装的运行时开销,则可以按照@LuisMiguelMejíaSuárez的建议尝试@newtype。
import io.estatico.newtype.macros.newtype
@newtype case class UuidA(value: String Refined UuidPredicate)
@newtype case class UuidB(value: String Refined UuidPredicate)或者尝试添加更多的标记
import eu.timepit.refined.api.Refined
import eu.timepit.refined.string.Uuid
import eu.timepit.refined.auto._
import shapeless.tag
import shapeless.tag.@@
type UuidPredicate = Uuid
type UuidString = Refined[String, UuidPredicate]
type TagA
type TagB
type UuidA = UuidString @@ TagA
type UuidB = UuidString @@ TagB
case class Product(a: UuidA, b: UuidB)
val myUuid: UuidA = tag[TagA][UuidString]("9f9ef0c6-b6f8-11ea-b3de-0242ac130004")
// val product = Product(myUuid, myUuid) // doesn't compile
val myUuid1: UuidB = tag[TagB][UuidString]("9f9ef0c6-b6f8-11ea-b3de-0242ac130004")
val product1 = Product(myUuid, myUuid1) // compileshttps://stackoverflow.com/questions/62579057
复制相似问题