ArrowAssoc / AnyVal question

Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

ArrowAssoc / AnyVal question

Ryan Williams
My understanding of value-classes led me to believe that usages of the "->" from ArrowAssoc would be compiled out and run as if Tuple2's had been instantiated directly.

However, when I feed this file to scalac (2.12.1):

// Test.scala
object Test {
  val tuple = "aaa" -> "bbb"
}

I see instances of ArrowAssoc in the resulting bytecode.

Here's a gist with the .scala and .class files as well as the ASMified .class file.

What am I missing?

--
You received this message because you are subscribed to the Google Groups "scala-language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: ArrowAssoc / AnyVal question

Jasper-M
Hi Ryan,

object Test {
  val tuple = "aaa" -> "bbb"
}

is transformed to something like

object Test {
  val tuple = Predef.ArrowAssoc.->(Predef.ArrowAssoc("aaa"), "bbb")
}

and the conversion Predef.ArrowAssoc is defined like this:

def ArrowAssoc[A](a: A): A = a

So no new instances are created. All that happens is a method call to -> on a singleton object and a method call to an identity function. 
I agree that that extra method call could be avoided, but I think it's an artifact of how implicit conversions and value classes are compiled in different stages. And it will probably be optimized away at runtime anyway.

Kind regards,
Jasper

Op dinsdag 7 maart 2017 17:52:22 UTC+1 schreef Ryan Williams:
My understanding of value-classes led me to believe that usages of <a href="https://github.com/scala/scala/blob/v2.12.1/src/library/scala/Predef.scala#L308" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Fscala%2Fscala%2Fblob%2Fv2.12.1%2Fsrc%2Flibrary%2Fscala%2FPredef.scala%23L308\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHTNCcoFd-EKp5mWTs68xQNb6Kkyg&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Fscala%2Fscala%2Fblob%2Fv2.12.1%2Fsrc%2Flibrary%2Fscala%2FPredef.scala%23L308\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHTNCcoFd-EKp5mWTs68xQNb6Kkyg&#39;;return true;">the "->" from ArrowAssoc would be compiled out and run as if Tuple2's had been instantiated directly.

However, when I feed this file to scalac (2.12.1):

// Test.scala
object Test {
  val tuple = "aaa" -> "bbb"
}

I see instances of ArrowAssoc in the resulting bytecode.

<a href="https://gist.github.com/ryan-williams/20b96b78c3414e7292302a9e6f799e8a" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgist.github.com%2Fryan-williams%2F20b96b78c3414e7292302a9e6f799e8a\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGiUeAB_CFmb8HEdkeVtLoRQitFvw&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgist.github.com%2Fryan-williams%2F20b96b78c3414e7292302a9e6f799e8a\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGiUeAB_CFmb8HEdkeVtLoRQitFvw&#39;;return true;">Here's a gist with the .scala and .class files as well as the ASMified .class file.

What am I missing?

--
You received this message because you are subscribed to the Google Groups "scala-language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.