Discussion:
[scala-language] Expected warning for @volatile def
Benjamin Darfler
2016-07-15 14:22:52 UTC
Permalink
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 scala-language+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Adriaan Moors
2016-07-26 22:20:58 UTC
Permalink
Yes, I agree the compiler should warn more consistently about nonsensical
use of @volatile.
I filed this at https://github.com/scala/scala-dev/issues/190
Post by Benjamin Darfler
Hello all,
@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();
}
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 scala-language+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...