Annotations associate meta-information with definitions.
A simple annotation has the form
@$c(a_1 , \ldots , a_n)$.
Here, $c$ is a constructor of a class $C$, which must conform
to the class
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.
Java Platform Annotations
The meaning of annotation clauses is implementation-dependent. On the Java platform, the following annotations have a standard meaning.
@transientMarks a field to be non-persistent; this is equivalent to the
transientmodifier in Java.
@volatileMarks a field which can change its value outside the control of the program; this is equivalent to the
volatilemodifier in Java.
@SerialVersionUID(<longlit>)Attaches a serial version identifier (a
longconstant) to a class. This is equivalent to a the following field definition in Java:
@throws(<classlit>)A Java compiler checks that a program contains handlers for checked exceptions by analyzing which checked exceptions can result from execution of a method or constructor. For each checked exception which is a possible result, the
throwsclause for the method or constructor must mention the class of that exception or one of the superclasses of the class of that exception.
Java Beans Annotations
@scala.beans.BeanPropertyWhen prefixed to a definition of some variable
X, this annotation causes getter and setter methods
setXin the Java bean style to be added in the class containing the variable. The first letter of the variable appears capitalized after the
set. When the annotation is added to the definition of an immutable value definition
X, only a getter is generated. The construction of these methods is part of code-generation; therefore, these methods become visible only once a classfile for the containing class is generated.
@scala.beans.BooleanBeanPropertyThis annotation is equivalent to
scala.reflect.BeanProperty, but the generated getter method is named
@deprecated(message: <stringlit>, since: <stringlit>)
Marks a definition as deprecated. Accesses to the defined entity will then cause a deprecated warning mentioning the message
<stringlit>to be issued from the compiler. The argument since documents since when the definition should be considered deprecated.
Deprecated warnings are suppressed in code that belongs itself to a definition that is labeled deprecated.
Marks a formal parameter name as deprecated. Invocations of this entity using named parameter syntax referring to the deprecated parameter name cause a deprecation warning.
Scala Compiler Annotations
@uncheckedWhen applied to the selector of a
matchexpression, this attribute suppresses any warnings about non-exhaustive pattern matches which would otherwise be emitted. For instance, no warnings would be produced for the method definition below.
@uncheckedannotation, a Scala compiler could infer that the pattern match is non-exhaustive, and could produce a warning because
@uncheckedStableWhen applied a value declaration or definition, it allows the defined value to appear in a path, even if its type is volatile. For instance, the following member definitions are legal:
@uncheckedStableannotation, the designator
xwould not be a path since its type
A with Bis volatile. Hence, the reference
x.Twould be malformed.
When applied to value declarations or definitions that have non-volatile types, the annotation has no effect.
@specializedWhen applied to the definition of a type parameter, this annotation causes the compiler to generate specialized definitions for primitive types. An optional list of primitive types may be given, in which case specialization takes into account only those types. For instance, the following code would generate specialized traits for
Whenever the static type of an expression matches a specialized variant of a definition, the compiler will instead use the specialized version. See the specialization sid for more details of the implementation.
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.StaticAnnotation. If an annotation class inherits from
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.