E193: Volatile on Val
This warning is emitted when the @volatile annotation is applied to a val (immutable value).
The @volatile annotation is meaningful only for mutable fields (var) because it ensures visibility of changes across threads. Since val is immutable and can only be assigned once during initialization, the volatile annotation has no effect and is likely a mistake.
Example
class Example:
@volatile val x: Int = 42
Error
-- [E193] Syntax Warning: example.scala:2:16 -----------------------------------
2 | @volatile val x: Int = 42
| ^
| values cannot be volatile
Solution
If you need a volatile field, use var:
class Example:
@volatile var x: Int = 42
If the value should be immutable, remove the @volatile annotation:
class Example:
val x: Int = 42
In this article