estatico / scala-newtype

NewTypes for Scala with no runtime overhead
Apache License 2.0
540 stars 31 forks source link

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

Open berserk-fan opened 1 year ago

berserk-fan commented 1 year ago

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?

joroKr21 commented 1 year ago

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

kciesielski commented 4 months ago

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?

joroKr21 commented 4 months ago

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