Scala and the Java Memory Model

The Java Memory Model (since 1.5) treats final fields differently to non-final fields. In particular, provided the this reference doesn't escape during construction, writes to final fields in the constructor are guaranteed to be visible on other threads even if the object is made available to the other thread via a data race. (Writes to non-final fields aren't guaranteed to be visible, so if you improperly publish them, another thread could see them in a partially constructed state.)

Is there any documentation on how/if the Scala compiler creates final (rather than non-final) backing fields for classes? I've looked through the language specification and searched the web but can't find any definitive answers. (In comparison the @scala.volatile annotation is documented to mark a field as volatile)

Answers


I dug through the history to find out when the change was made.

The projection of Scala into the JVM isn't covered by the language specification.


It creates a final field when you declare something as a val. Anything whose references can be modified, such as a var, can (obviously) not be final underneath.

This means that case classes contain final fields also (as the arguments to a case class constructor are implicitly vals)


I've filed a documentation bug for this in the Scala bug system.


Need Your Help

How to Store Tweets in HDFS?

twitter hadoop hdfs sqoop flume

How to Store particular website tweets in HDFS ?

Add S3 with Carrierwave to Rails App for Heroku

ruby heroku amazon-s3 ruby-on-rails-4 carrierwave

I'm trying to add S3 to my Heroku app however, I'm getting the same problem I had on a previous question (carrierwave image not loading into source code) where the image url isn't loading into the ...