Scala Annotations are metadata added to the program source code. Annotations are allowed on any kind of definition or declaration including vals, vars, classes, objects, traits, defs and types. Annotations are used to associate meta-information with definitions.
Syntax:
@annot(exp_{1}, exp_{2}, …) {val name_{1}=const_{1}, …, val name_{n}=const_{n}}
All annotations must include “annot” as it specifies the annotations class. The annotations which have no arguments uses an empty().
Predefined Annotations in Scala
Predefined annotations in Scala are built-in scala annotations which associate meta-information with definition. Some of the predefined scala annotations are:
- Java Platform Annotations:
- Java Beans Annotations:
- @scala.beans.BeanProperty: This annotation, when prefixed to a definition of some variable X, causes getter and setter methods getX, setX in the Java bean style to be added in the class containing the variable. first letter of a variable seems capitalized after using the get or set.
- @scala.beans.BooleanBeanProperty: This annotation is equivalent to scala.reflect.BeanProperty, but the generated getter method is named isX not getX.
- Deprecation Annotations:
- Scala Compiler Annotations:
User-defined Annotations in Scala
User-defined annotations associate meta-information with definitions. For a class, Class files hold an annotation class instances which class inherits from the trait Scala.ClassfileAnnotation. the instances are visible to the Scala type-checker for every compilation unit where we access the annotated symbol and class inherits from the trait scala.StaticAnnotation.
Syntax:
// needed for @Documented
import java.lang.annotation.*;
@Documented
public @interface impure {}
Last Updated :
08 Apr, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...