Iltotore / iron

Strong type constraints for Scala
https://iltotore.github.io/iron/docs/
Apache License 2.0
427 stars 37 forks source link

Question: exporting an alias for use in dependent projects, to use with constructor syntax without needing an `iron` import #250

Closed vreuter closed 5 days ago

vreuter commented 6 days ago

Hello, I have an intermediary-like library in which I'd like to defined, say, type NonnegativeInt = Int :| Not[Negative] or type NonnegativeInt = Int :| GreaterEqual[0], and then be able to do something like val ref: NonnegativeInt = NonnegativeInt(0). I've gotten this working with creating a object NonnegativeInt companion that extends RefinedTypeOps, but if and only if I have autoRefine in scope at the call site, via e.g. import io.github.iltotore.iron.* or import io.github.iltotore.iron.autoRefine.

Is it intended that call sites would need such an import to use the constructor-like syntax provided by RefinedTypeOps? Am I missing something that would make it possible for me to define a numerics-like library that then could be used by downstream projects without them needing to know about the iron behind-the-scenes?

Iltotore commented 6 days ago

Hello.

This is the intented behaviour. Back when RefinedTypeOps was introduced, there was no such limitation but it caused other issues and after some tests I decided to go with the current design. It is still possible to make your own version of RefinedTypeOps (the code is not very long) and change the apply method.

Fortunately this issue might be resolved when Iron will be able to upgrade to newer Scala versions with the use of the erase keyword.

vreuter commented 5 days ago

Many thanks for the reply and explanation @Iltotore .