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 above very common opaque type use case won't compile.
Seems compiler don't know Id is Long while deriving.
Note, if opaque type defined in top level, it compiles just fine.
importio.circe._opaquetypeFooId=LongcaseclassFoo(id: FooId) derivesCodec.AsObject// Compiles ok
The text was updated successfully, but these errors were encountered:
jilen
changed the title
Allow summon instance of opaque type defined in companion object while deriving.
Allow summon typeclass instance of opaque type defined in companion object while deriving.
Apr 14, 2023
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".
Consider the following:
opaquetypePositiveInt=Intdefpositive(i: Int):Option[PositiveInt] =Option.when(i >0)(i)
deftoInt(p: PositiveInt):Int= p
Not pretty sure, but I thought opaque type is basically used as newtype instead of refined type.
However, Refined library is implemented exactly as opaque types for scala3. And many typeclasses (e.g. io.circe.Decoder, sttp.tapir.Shema, com.monovore.decline.Argument has extra modules to derive their typeclasses properly for Refined types, respecting their "business-logic".
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...
So /issues/17262 is the root cause and the thing to fix
ckipp01
transferred this issue from lampepfl/dotty-feature-requests
May 31, 2023
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.
The text was updated successfully, but these errors were encountered: