User-Defined Annotations

  Annotation       ::=  ‘@’ SimpleType {ArgumentExprs}
  ConstrAnnotation ::=  ‘@’ SimpleType ArgumentExprs

User-defined annotations associate meta-information with definitions. A simple annotation has the form @$c$ or @$c(a_1 , \ldots , a_n)$. Here, $c$ is a constructor of a class $C$, which must conform to the class scala.Annotation.

Annotations may apply to definitions or declarations, types, or expressions. An annotation of a definition or declaration appears in front of that definition. An annotation of a type appears after that type. An annotation of an expression $e$ appears after the expression $e$, separated by a colon. More than one annotation clause may apply to an entity. The order in which these annotations are given does not matter.

Examples:

@deprecated("Use D", "1.0") class C { ... } // Class annotation
@transient @volatile var m: Int             // Variable annotation
String @local                               // Type annotation
(e: @unchecked) match { ... }               // Expression annotation

The meaning of annotation clauses is implementation-dependent. On the Java platform, the following annotations have a standard meaning.

Java Beans Annotations

Deprecation Annotations

Scala Compiler Annotations

Other annotations may be interpreted by platform- or application-dependent tools. Class scala.Annotation has two sub-traits which are used to indicate how these annotations are retained. Instances of an annotation class inheriting from trait scala.ClassfileAnnotation will be stored in the generated class files. Instances of an annotation class inheriting from trait scala.StaticAnnotation will be visible to the Scala type-checker in every compilation unit where the annotated symbol is accessed. An annotation class can inherit from both scala.ClassfileAnnotation and scala.StaticAnnotation. If an annotation class inherits from neither scala.ClassfileAnnotation nor scala.StaticAnnotation, its instances are visible only locally during the compilation run that analyzes them.

Classes inheriting from scala.ClassfileAnnotation may be subject to further restrictions in order to assure that they can be mapped to the host environment. In particular, on both the Java and the .NET platforms, such classes must be toplevel; i.e. they may not be contained in another class or object. Additionally, on both Java and .NET, all constructor arguments must be constant expressions.