-
|
I'd like to use Iron for newtypes, for now just to prevent usages of A in place of B, when A and B have the same type but different meanings in my domain. I tried one of the examples from the docs, but I don't get a compile error where I expected one: import io.github.iltotore.iron.*
import io.github.iltotore.iron.constraint.numeric.Positive
opaque type Temperature = Double :| Positive
object Temperature extends RefinedTypeOps[Double, Positive, Temperature]
opaque type Moisture = Double :| Positive
object Moisture extends RefinedTypeOps[Double, Positive, Moisture]
case class Info(temperature: Temperature, moisture: Moisture)
@main
def main(): Unit =
val temperature: Temperature = Temperature(20.0)
val moisture: Moisture = Moisture(0.5)
val info = Info(moisture, temperature) // should not compile but does?
println(info)I use iron version 2.6.0 and Scala 3.4.2. I compiled it via sbt. The example is from the iron docs. this compiles as well for me, it's from the other example in the docs import io.github.iltotore.iron.*
import io.github.iltotore.iron.constraint.all.Positive
object Test:
opaque type Temperature = Double :| Positive
object Temperature extends RefinedTypeOps[Double, Positive, Temperature]
val x: Double :| Positive = 5
val temperature: Temperature = x // compiles as wellis there something I'm missing? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
Hello. This is due to how opaque types work. They are only opaque outside of their definition scope (like in a different file) and that's why the example is split in two code samples (tho it might be useful to add a reminder about this scope mechanism). Here is a working version in Scastie. Since it (Scastie) does not support multiple files, I wrapped the types in an object: import io.github.iltotore.iron.*
import io.github.iltotore.iron.constraint.numeric.Positive
object types:
opaque type Temperature = Double :| Positive
object Temperature extends RefinedTypeOps[Double, Positive, Temperature]
opaque type Moisture = Double :| Positive
object Moisture extends RefinedTypeOps[Double, Positive, Moisture]
import types.*
case class Info(temperature: Temperature, moisture: Moisture)
@main
def main(): Unit =
val temperature: Temperature = Temperature(20.0)
val moisture: Moisture = Moisture(0.5)
val info = Info(moisture, temperature) // should not compile but does?
val info2 = Info(temperature, moisture) // OK
println(info) |
Beta Was this translation helpful? Give feedback.
Hello. This is due to how opaque types work. They are only opaque outside of their definition scope (like in a different file) and that's why the example is split in two code samples (tho it might be useful to add a reminder about this scope mechanism).
Here is a working version in Scastie. Since it (Scastie) does not support multiple files, I wrapped the types in an object: