You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The Coercible instances currently generated by the @newtype macro are always publicly available in implicit scope. I think this makes sense when defining a newtype as a case class since a standard apply method will be generated, but when defining one as a class with a custom apply method, it allows external code to bypass any validation logic in that method. For example
// types.scalapackageobjecttypes {
@newtype classPositiveInt(i: Int)
objectPositiveInt {
defapply(i: Int):Option[PositiveInt] =if (i >0) Some(i.coerce[PositiveInt]) elseNone
}
}
// test.scala -- this compiles but ideally it wouldn't-1.coerce[types.PositiveInt]
Would it be possible to make the Coercible instances for a class only visible in implicit scope in the companion object? Or if that seems like too broad of a change, perhaps the logic could only apply when the class constructor is explicitly marked as private, e.g. @newtype class PositiveInt private (i: Int)?
Thank you for all your work on this library!
The text was updated successfully, but these errors were encountered:
The
Coercible
instances currently generated by the@newtype
macro are always publicly available in implicit scope. I think this makes sense when defining a newtype as acase class
since a standardapply
method will be generated, but when defining one as aclass
with a customapply
method, it allows external code to bypass any validation logic in that method. For exampleWould it be possible to make the
Coercible
instances for aclass
only visible in implicit scope in the companion object? Or if that seems like too broad of a change, perhaps the logic could only apply when theclass
constructor is explicitly marked as private, e.g.@newtype class PositiveInt private (i: Int)
?Thank you for all your work on this library!
The text was updated successfully, but these errors were encountered: