Re: ArrowAssoc / AnyVal question

Jasper-M <[email protected]> Wed, 8 Mar 2017 04:49:23 -0800 (PST)
Newsgroups gmane.comp.lang.scala
Message-ID <[email protected]>
------=_Part_167_1256047199.1488977363136
Content-Type: multipart/alternative; 
	boundary="----=_Part_168_357333694.1488977363137"

------=_Part_168_357333694.1488977363137
Content-Type: text/plain; charset=UTF-8

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 the 
> "->" from ArrowAssoc 
> <https://github.com/scala/scala/blob/v2.12.1/src/library/scala/Predef.scala#L308> 
> 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 
> <https://gist.github.com/ryan-williams/20b96b78c3414e7292302a9e6f799e8a> 
> 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 [email protected].
For more options, visit https://groups.google.com/d/optout.

------=_Part_168_357333694.1488977363137
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Hi Ryan,<div><br></div><div><div><font face=3D"courier new=
, monospace">object Test {</font></div><div><font face=3D"courier new, mono=
space">=C2=A0 val tuple =3D &quot;aaa&quot; -&gt; &quot;bbb&quot;</font></d=
iv><div><font face=3D"courier new, monospace">}</font></div><div><br></div>=
<div>is transformed to something like</div><div><br></div><div><div><font f=
ace=3D"courier new, monospace">object Test {</font></div><div><font face=3D=
"courier new, monospace">=C2=A0 val tuple =3D Predef.ArrowAssoc.-&gt;(Prede=
f.ArrowAssoc(&quot;aaa&quot;), &quot;bbb&quot;)</font></div><div><font face=
=3D"courier new, monospace">}</font></div></div><div><br></div><div>and the=
 conversion Predef.ArrowAssoc is defined like this:</div><div><br></div><di=
v><font face=3D"courier new, monospace">def ArrowAssoc[A](a: A): A =3D a</f=
ont></div><div><font face=3D"courier new, monospace"><br></font></div><div>=
<font face=3D"arial, sans-serif">So no new instances are created. All that =
happens is a method call to -&gt; on a singleton object and a method call t=
o an identity function.=C2=A0</font></div><div><font face=3D"arial, sans-se=
rif">I agree that that extra method call could be avoided, but I think it&#=
39;s an artifact of how implicit conversions and value classes are compiled=
 in different stages. And it will probably be optimized away at runtime any=
way.</font></div><div><font face=3D"arial, sans-serif"><br></font></div><di=
v><font face=3D"arial, sans-serif">Kind regards,</font></div><div><font fac=
e=3D"arial, sans-serif">Jasper</font></div><br>Op dinsdag 7 maart 2017 17:5=
2:22 UTC+1 schreef Ryan Williams:<blockquote class=3D"gmail_quote" style=3D=
"margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex=
;"><div dir=3D"ltr">My understanding of value-classes led me to believe tha=
t usages of <a href=3D"https://github.com/scala/scala/blob/v2.12.1/src/libr=
ary/scala/Predef.scala#L308" target=3D"_blank" rel=3D"nofollow" onmousedown=
=3D"this.href=3D&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.c=
om%2Fscala%2Fscala%2Fblob%2Fv2.12.1%2Fsrc%2Flibrary%2Fscala%2FPredef.scala%=
23L308\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHTNCcoFd-EKp5mWTs68xQNb6Kkyg=
&#39;;return true;" onclick=3D"this.href=3D&#39;https://www.google.com/url?=
q\x3dhttps%3A%2F%2Fgithub.com%2Fscala%2Fscala%2Fblob%2Fv2.12.1%2Fsrc%2Flibr=
ary%2Fscala%2FPredef.scala%23L308\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNH=
TNCcoFd-EKp5mWTs68xQNb6Kkyg&#39;;return true;">the &quot;-&gt;&quot; from A=
rrowAssoc</a> would be compiled out and run as if Tuple2&#39;s had been ins=
tantiated directly.<div><br></div><div>However, when I feed this file to sc=
alac (2.12.1):</div><div><br></div><div><font face=3D"monospace">// Test.sc=
ala</font></div><div><div><font face=3D"monospace">object Test {</font></di=
v><div><font face=3D"monospace">=C2=A0 val tuple =3D &quot;aaa&quot; -&gt; =
&quot;bbb&quot;</font></div><div><font face=3D"monospace">}</font></div></d=
iv><div><br></div><div>I see instances of ArrowAssoc in the resulting bytec=
ode.</div><div><br></div><div><a href=3D"https://gist.github.com/ryan-willi=
ams/20b96b78c3414e7292302a9e6f799e8a" target=3D"_blank" rel=3D"nofollow" on=
mousedown=3D"this.href=3D&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2=
Fgist.github.com%2Fryan-williams%2F20b96b78c3414e7292302a9e6f799e8a\x26sa\x=
3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGiUeAB_CFmb8HEdkeVtLoRQitFvw&#39;;return t=
rue;" onclick=3D"this.href=3D&#39;https://www.google.com/url?q\x3dhttps%3A%=
2F%2Fgist.github.com%2Fryan-williams%2F20b96b78c3414e7292302a9e6f799e8a\x26=
sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGiUeAB_CFmb8HEdkeVtLoRQitFvw&#39;;retu=
rn true;">Here&#39;s a gist</a> with the .scala and .class files as well as=
 the ASMified .class file.</div><div><br></div><div>What am I missing?</div=
></div>
</blockquote></div></div>

<p></p>

-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;scala-language&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:[email protected]">scal=
[email protected]</a>.<br />
For more options, visit <a href=3D"https://groups.google.com/d/optout">http=
s://groups.google.com/d/optout</a>.<br />

------=_Part_168_357333694.1488977363137--

------=_Part_167_1256047199.1488977363136--