I am new to scala and the refined library but I am trying to create two refined types based on UUIDs.
In order to do so, I did this (Note: Uuid in this case comes from eu.timepit.refined.string.Uuid):
type UuidPredicate = Uuid
type UuidA = String Refined UuidPredicate
type UuidB = String Refined UuidPredicate
However, it appears as though this only creates aliases, and therefore there is no type safety.
So if I had a constructor like Product(a UuidA, b UuidB)
and proceeded to do something like this:
val myUuid: UuidA = "9f9ef0c6-b6f8-11ea-b3de-0242ac130004"
val Product = Product(myUuid, myUuid)
It would compile and run properly. Is there anyway to ensure this is not the case? If a variable is created as one type, how can I make it so it only be used as that particular refined type, even though the types are fundamentally the same?
The simplest is to introduce different data types
You can't make
UuidA,UuidBextendAnyValbecauseRefinedalready extendsAnyValand Scala doesn't allow nested value classes.If you prefer to avoid runtime overhead of wrapping with
UuidA,UuidByou can try@newtypeas @LuisMiguelMejíaSuárez advicedOr try to add more tags