Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags


What are annotations in Scala?

Dian Us Suqlain

Scala categorizes annotations as meta-dataa set of data describing and giving information about other data or supplement information added to a program source code. Annotations can apply to any declaration or definition (i.e., instance variables, constructors, methods, classes, types, etc.)

Annotations always start with the @ symbol, and they do not change the action of a compiled program. Since Scala integrates with Java’s annotations system, it is easier to call built-in annotations from Java.

We can only apply an annotation clause to the first definition or declaration. After that, the order of annotation clauses doesn’t matter. Thus, more than one annotation clause can precede a definition and declaration.

Built-in and Meta-annotations

Users can either define their own annotations (i.e., User-defined annotations) or use the built-in annotations from the Scala library.

Some built-in annotations in Scala include:

  • @deprecated: This indicates that a declaration is obsolete and has been replaced by a newer form.

  • @unchecked: This attribute suppresses any warnings about non-exhaustive pattern matches which would otherwise be emitted.

  • @transient: This annotation is used to make a field non-persistent.

  • @volatile: This marks a field that can change its value outside the control of the program.

  • @tailrec: This annotation ensures that a method is tail-recursive.

For further knowledge about annotations in Scala, you can read the official documentation here.




Dian Us Suqlain
Copyright ©2022 Educative, Inc. All rights reserved

View all Courses

Keep Exploring