ArrowAssoc / AnyVal question

54 views
Skip to first unread message

Ryan Williams

unread,
Mar 7, 2017, 11:52:22 AM3/7/17
to scala-l...@googlegroups.com
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?

Jasper-M

unread,
Mar 8, 2017, 7:49:23 AM3/8/17
to scala-language
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:
Reply all
Reply to author
Forward
0 new messages