Allow summon typeclass instance of opaque type defined in companion object while deriving. #17640
Replies: 3 comments
-
I think the one I created might be related: https://github.com/lampepfl/dotty-feature-requests/issues/344 I personally think that type classes for opaque types shouldn't automatically pass-through to underlying type, as it's breaking the concept of a "different type". opaque type PositiveInt = Int
def positive(i: Int): Option[PositiveInt] = Option.when(i > 0)(i)
def toInt(p: PositiveInt): Int = p |
Beta Was this translation helpful? Give feedback.
-
Not pretty sure, but I thought opaque type is basically used as |
Beta Was this translation helpful? Give feedback.
-
However, In general I'm trying to say that by-passing typeclasses to underlying type shouldn't be automatic as it will make them unsafe - the intention of opaque type as I see it - is to encapsulate the underlying type at zero runtime cost. If no encapsulation is cared, then why not using a simple type alias (without opaque keyword)? But as for companion-object case, I agree that by-passing opaque typeclass for derivation looks eligible, because the opaque type defined right in the companion object which (I believe) is the scope of derivation... |
Beta Was this translation helpful? Give feedback.
-
The above very common opaque type use case won't compile.
Seems compiler don't know
Id
isLong
while deriving.Note, if opaque type defined in top level, it compiles just fine.
Beta Was this translation helpful? Give feedback.
All reactions