estatico / scala-newtype

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

More Documentation #16

Open TimWSpence opened 6 years ago

TimWSpence commented 6 years ago

Sorry, this isn't an issue as such, I was just looking for more information on how newtype works. The README says something like this is generated:

package object types {
  type WidgetId = WidgetId.Type
  object WidgetId {
    type Repr = Int
    type Base = { type WidgetId$newtype }
    trait Tag
    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]
    }
  }
}

I get the basic idea that you use macros to generate a value class but I wasn't sure why so many intermediate types are defined or what type Base = { type WidgetId$newtype }, which looks abstract, did?

Any more info would be much appreciated, thanks!!!