What annotations do
Scala annotations attach metadata or compiler instructions to declarations. Although they use an @ prefix, they do not work like Python decorators, which can wrap or replace the decorated object.
Some annotations guide compilation; others support tools or runtime processing. Whether an annotation can be inspected at runtime depends on its type and retention. Do not assume every Scala annotation is available through Java reflection.
Suppressing compiler warnings
import scala.annotation.nowarn
@nowarn
def myMethod(): Unit = {
// Compiler warnings in this definition are suppressed.
}
@nowarn does not suppress runtime exceptions or log messages. Prefer a narrow warning filter when only one warning is intentional.
Common annotations
@tailrec: asks the compiler to verify that the recursive method can be optimized into a loop; compilation fails if it cannot. The annotation does not rewrite an arbitrary recursive algorithm.@volatile: on the JVM, gives a field Java volatile visibility and ordering semantics. A compound operation such ascounter += 1is still not atomic.@transient: excludes a field from Java’s default object serialization; other serializers may follow their own rules.@native: declares a method whose implementation is supplied by native code.
References: Scala’s annotation guide, annotation API, and JVM volatile semantics.