Expected warning for @volatile def
Benjamin Darfler <[email protected]> Fri, 15 Jul 2016 07:22:52 -0700 (PDT)
| Newsgroups | gmane.comp.lang.scala |
|---|---|
| Message-ID | <[email protected]> |
------=_Part_176_624619988.1468592572952
Content-Type: multipart/alternative;
boundary="----=_Part_177_1269100203.1468592572959"
------=_Part_177_1269100203.1468592572959
Content-Type: text/plain; charset=UTF-8
Hello all,
I recently came across the following code in our codebase:
@volatile def doSomething = { /** do the thing */ }
I was immediately suspicious and created the following test
class Test {
@volatile private[this] var something = "something"
@volatile def doSomething = { println(something) }
}
$ scalac test.scala
$ javap -p Test.class
Compiled from "test.scala"
public class Test {
private volatile java.lang.String something;
public void doSomething();
public Test();
}
As you can see the @volatile on the var translates into a volatile variable
in Java but the @volatile on the def is silently dropped.
This makes sense since a @volatile def is nonsensical but I was surprised
that there was no compiler warning for this.
Should this be considered a bug?
Ben Darfler
--
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_177_1269100203.1468592572959
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">Hello all,<div><br></div><div>I recently came across the f=
ollowing code in our codebase:</div><div><br></div><div><div class=3D"prett=
yprint" style=3D"border: 1px solid rgb(187, 187, 187); word-wrap: break-wor=
d; background-color: rgb(250, 250, 250);"><code class=3D"prettyprint"><div =
class=3D"subprettyprint"><font color=3D"#660066"><span style=3D"color: #066=
;" class=3D"styled-by-prettify">@volatile</span><span style=3D"color: #000;=
" class=3D"styled-by-prettify"> </span><span style=3D"color: #008;" class=
=3D"styled-by-prettify">def</span><span style=3D"color: #000;" class=3D"sty=
led-by-prettify"> doSomething </span><span style=3D"color: #660;" class=3D"=
styled-by-prettify">=3D</span><span style=3D"color: #000;" class=3D"styled-=
by-prettify"> </span><span style=3D"color: #660;" class=3D"styled-by-pretti=
fy">{</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </sp=
an><span style=3D"color: #800;" class=3D"styled-by-prettify">/** do the thi=
ng */</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </sp=
an><span style=3D"color: #660;" class=3D"styled-by-prettify">}</span></font=
><span style=3D"color: #000;" class=3D"styled-by-prettify"><br></span></div=
></code></div><br>I was immediately=C2=A0suspicious and created the followi=
ng test</div><div><br></div><div><div class=3D"prettyprint" style=3D"border=
: 1px solid rgb(187, 187, 187); word-wrap: break-word; background-color: rg=
b(250, 250, 250);"><code class=3D"prettyprint"><div class=3D"subprettyprint=
"><p class=3D"p1"><span class=3D"s1"><span style=3D"color: #008;" class=3D"=
styled-by-prettify">class</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify"> </span><span style=3D"color: #606;" class=3D"styled-by-pret=
tify">Test</span><span style=3D"color: #000;" class=3D"styled-by-prettify">=
</span><span style=3D"color: #660;" class=3D"styled-by-prettify">{</span><=
/span></p><span style=3D"color: #000;" class=3D"styled-by-prettify"><br></s=
pan><p class=3D"p1"><span class=3D"s1"><span style=3D"color: #000;" class=
=3D"styled-by-prettify">=C2=A0 </span><span style=3D"color: #066;" class=3D=
"styled-by-prettify">@volatile</span><span style=3D"color: #000;" class=3D"=
styled-by-prettify"> </span><span style=3D"color: #008;" class=3D"styled-by=
-prettify">private</span><span style=3D"color: #660;" class=3D"styled-by-pr=
ettify">[</span><span style=3D"color: #008;" class=3D"styled-by-prettify">t=
his</span><span style=3D"color: #660;" class=3D"styled-by-prettify">]</span=
><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span st=
yle=3D"color: #008;" class=3D"styled-by-prettify">var</span><span style=3D"=
color: #000;" class=3D"styled-by-prettify"> something </span><span style=3D=
"color: #660;" class=3D"styled-by-prettify">=3D</span><span style=3D"color:=
#000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #080;" c=
lass=3D"styled-by-prettify">"something"</span></span></p><span st=
yle=3D"color: #000;" class=3D"styled-by-prettify"><br></span><p class=3D"p1=
"><span class=3D"s1"><span style=3D"color: #000;" class=3D"styled-by-pretti=
fy">=C2=A0 </span><span style=3D"color: #066;" class=3D"styled-by-prettify"=
>@volatile</span><span style=3D"color: #000;" class=3D"styled-by-prettify">=
</span><span style=3D"color: #008;" class=3D"styled-by-prettify">def</span=
><span style=3D"color: #000;" class=3D"styled-by-prettify"> doSomething </s=
pan><span style=3D"color: #660;" class=3D"styled-by-prettify">=3D</span><sp=
an style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">{</span><span style=3D"color=
: #000;" class=3D"styled-by-prettify"> println</span><span style=3D"color: =
#660;" class=3D"styled-by-prettify">(</span><span style=3D"color: #000;" cl=
ass=3D"styled-by-prettify">something</span><span style=3D"color: #660;" cla=
ss=3D"styled-by-prettify">)</span><span style=3D"color: #000;" class=3D"sty=
led-by-prettify"> </span><span style=3D"color: #660;" class=3D"styled-by-pr=
ettify">}</span></span></p><p class=3D"p2"><span style=3D"color: #000;" cla=
ss=3D"styled-by-prettify"><br></span><span class=3D"s1"></span></p><p class=
=3D"p1"><span class=3D"s1"><span style=3D"color: #660;" class=3D"styled-by-=
prettify">}</span></span></p></div></code></div><br></div><br>$ scalac test=
.scala <br><br>$ javap -p Test.class <br><br>Compiled from "test.scala=
" <br><br>public class Test { <br><br>private volatile java.lang.Strin=
g something; <br><br>public void doSomething(); <br><br>public Test(); <br>=
<br>}<div><br></div>As you can see the @volatile on the var translates into=
a volatile variable in Java but the @volatile on the def is silently dropp=
ed.=C2=A0<div><br></div><div>This makes sense since a @volatile def is nons=
ensical but I was surprised that there was no compiler warning for this.<di=
v><br></div><div>Should this be considered a bug?</div></div><div><br></div=
><div>Ben Darfler</div></div>
<p></p>
-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;scala-language" 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_177_1269100203.1468592572959--
------=_Part_176_624619988.1468592572952--