scala-newtype icon indicating copy to clipboard operation
scala-newtype copied to clipboard

Newtype name is always equal to "Type" in compiler errors

Open berserk-fan opened this issue 1 year ago • 3 comments

Hi. Newtype name is always equal to "Type" in compiler errors. Example is Either[Type, Type] where it has to be Either[UserId, UserUid]. Is it possible to get proper names?

berserk-fan avatar Mar 24 '23 06:03 berserk-fan

Do you have an example? That might be caused by the Scala compiler rather than the library.

joroKr21 avatar Mar 24 '23 14:03 joroKr21

Bumping this, as we are experiencing a related issue in Tapir: https://github.com/softwaremill/tapir/issues/3835 If I understand correctly, with code generated like this:

package object types {
  type WidgetId = WidgetId.Type
  object WidgetId {
    type Repr = Int
    type Base = Any { type WidgetId$newtype }
    trait Tag extends Any
    type Type <: Base with Tag

    def apply(x: Int): WidgetId = x.asInstanceOf[WidgetId]

    implicit final class Ops$newtype(val $this$: Type) extends AnyVal {
      def toInt: Int = $this$.asInstanceOf[Int]
    }
  }
}

the WidgetId type is ultimately resolved as WidgetId.Type. This results in just the type name Type shown the compiler, as well as Tapir macros which resolve the name using typeSymbol.fullName (see here). Maybe the generated type could be more specific, something like

    type WidgetIdType <: Base with Tag

which shouldn't affect compatibility?

kciesielski avatar Jun 18 '24 16:06 kciesielski

type WidgetIdType <: Base with Tag erases to Object I think

joroKr21 avatar Jun 19 '24 04:06 joroKr21