These are chat archives for typelevel/general

22nd
Sep 2017
Alexander Konovalov
@alexknvl
Sep 22 2017 12:22

I don't know how it works, but :

object A {
  type WidgetId = WidgetId.Type
  object WidgetId extends NewType.Of[String] {
    def apply(i: String): WidgetId = Is[String, Type].apply(i)

    implicit val foo: Equiv[Type] = Is[String, Type].subst[Equiv](implicitly[Equiv[String]])
  }

  implicitly[Equiv[WidgetId]]
}

I modified https://github.com/estatico/scala-newtype a bit to use Is instead of Coercible

Unfortunately

class A {
  import A._

  def w: WidgetId = WidgetId(1)
}
object A {
  type WidgetId = WidgetId.Type
  object WidgetId extends NewSubType.Of[Int] {
    def apply(i: Int): WidgetId = i.asInstanceOf[WidgetId]
    ...
  }
  implicitly[WidgetId <:< Int]
}

boxes the int and then immediately unboxes it :(

Alexander Konovalov
@alexknvl
Sep 22 2017 12:28
         0: iload_1
         1: invokestatic  #40                 // Method scala/runtime/BoxesRunTime.boxToInteger:(I)Ljava/lang/Integer;
         4: invokestatic  #44                 // Method scala/runtime/BoxesRunTime.unboxToInt:(Ljava/lang/Object;)I
         7: ireturn
does JVM optimize such stupidity?
Edmund Noble
@edmundnoble
Sep 22 2017 12:30
Afaik yes
And the Scala compiler is supposed to as well
If you call with -optimize, probably
Alexander Konovalov
@alexknvl
Sep 22 2017 12:31
Ha, yes, it does
Edmund Noble
@edmundnoble
Sep 22 2017 12:41
Yeah I recommend looking at the big comment at the start of the optimizer, it explains the optimizations and their ordering
Lukas Rytz linked it somewhere earlier
Think the dotty channel