Expected warning for @volatile def

63 views
Skip to first unread message

Benjamin Darfler

unread,
Jul 15, 2016, 10:22:53 AM7/15/16
to scala-language
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

Adriaan Moors

unread,
Jul 26, 2016, 6:20:58 PM7/26/16
to scala-language
Yes, I agree the compiler should warn more consistently about nonsensical use of @volatile.
Reply all
Reply to author
Forward
0 new messages