This chapter we will introduce the annotations, annotations are structured added to program source code. Like comments , they can be sprinkled throughout a program and attached to any variable, method, expression, or other program. Unlike comments, they have structure, thus making them easier to machine process.
Why we need annotations.
such tools are called meta-programming code, becasue they are programs that take other programming as input, Annotions support these tools by letting the programmer
sprinkle directives to the tool throughout their source code.
- A documentation generator could be instructed to document certain methods are deprecated
- a pretty printer could be instructed to skip over parts of the program that have been carefully hand formatted.
- A checker for non-closed files could e instructed to ignore a particular file that has been manually verified to be closed.
- A side-effects checker could be instructed to verify that a specified method has no side-effects .
Syntax of annotations.
let's take the @depecation for examples.
@deprecated def bigMistake() = // ...
// a method is annotated as @deprecated. Annotation are allowed in other places, annotations are allowed on any kind of declaration or definitions .
// Trait, class, method, defs, objects, traits, vals, vars, and types.
@deprecated class QuickAndDirty {
// ...
}
and as with the @unchecked annotation for examples.
// annotation can also applieed to expressions.
(e : @unchecked) match {
// non-exhaustive errors...
}
and the general annot is the general annotation is like ?
// general form of annotation is
//
@annot(expr1, expr2)
the precise argument to the annotation can be any arbitrary expression, as long as they type check .
// @annot is like a new constructor calles.
@cool val normal = "hello"
@coolerThan(normal) val fronzy = "heeyyy"
while you cannot directly equal annotation to the constructor because the @annot is a expression.
// while you cannot directly equal annotation to the constructor
// because the @annot is a expression
//
import annotation._
class Strategy(arg : Annotation) extends Annotation
class delayed extends Annotation
@strategy(@delayed) def f() { }
@strategy(new delayed) def f() { }
Standard annontations
First is the @deprecated is like this:
// Deprecation
//
@deprecated def bigMistake() = // ...
@deprecated("use newShinyMethod() instead")
def bigMistake() = //...
to test the deprecation scalac
// scalac -depecation Deprecation2.scala
// you will get message on the deprecation
Volatile fileds
// Volatile fields
// this is for concurrent programming
Binary serialization
// Binary serialization
// @serializable
// serialize XML .
BeanProperty
// @BeanProperty
// @scala.reflect.BeanProperty annotation
// automatically get and set .
TailRec /tailrec tail recursive.
// @TailRec
// @tailrec
// tell the optimize the tail recursive.
Unchecked
// @unchecked
// it tells the compiler not to worry if the match expression seems to leave out some cases, See Section 15.5 for details.
Native methods
// Native methods
// @native anotations informs the compiler that a method's implemenation that is supplied by the runtime rather than in Scala code.
// and it will be up to the developer to supply the implementation using a mechanism such as the a java Native Interface (JNI).
//
@native
def beginCountdown() { }