Questions And Answers

More Tutorials

Scala Annotations


Using an Annotation

This sample annotation indicates that the following method is deprecated.

def anUnusedLegacyMethod(someArg: Any) = {

This can also be equivalently written as:

@deprecated def anUnusedLegacyMethod(someArg: Any) = {

Annotating the main constructor

 * @param num Numerator
* @param denom Denominator
 * @throws ArithmeticException in case `denom` is `0`
class Division @throws[ArithmeticException](/*no annotation parameters*/) protected (num: Int,
denom: Int) {
 private[this] val wrongValue = num / denom
 /** Integer number
 * @param num Value */
 protected[Division] def this(num: Int) {
 this(num, 1)
object Division {
 def apply(num: Int) = new Division(num)
 def apply(num: Int, denom: Int) = new Division(num, denom)

The visibility modifier (in this case protected) should come after the annotations in the same line. In case the annotation accepts optional parameters (as in this case @throws accepts an optional cause), you have to specify an empty parameter list for the annotation: () before the constructor parameters.

Note: Multiple annotations can be specified, even from the same type (repeating annotations).

Similarly with a case class without auxiliary factory method (and cause specified for the annotation):

case class Division @throws[ArithmeticException]("denom is 0") (num: Int, denom: Int) {
 private[this] val wrongValue = num / denom

Creating Your Own Annotations

You can create you own Scala annotations by creating classes derived from scala.annotation.StaticAnnotation or scala.annotation.ClassfileAnnotation

package animals
// Create Annotation `Mammal`
class Mammal(indigenous:String) extends scala.annotation.StaticAnnotation
// Annotate class Platypus as a `Mammal`
@Mammal(indigenous = "North America")
class Platypus{}

Annotations can then be interrogated using the reflection API.

scala>import scala.reflect.runtime.{universe ⇒ u}
scala>val platypusType = u.typeOf[Platypus]
platypusType: reflect.runtime.universe.Type = animals.reflection.Platypus
scala>val platypusSymbol = platypusType.typeSymbol.asClass
platypusSymbol: reflect.runtime.universe.ClassSymbol = class Platypus
List[reflect.runtime.universe.Annotation] = List(animals.reflection.Mammal("North America"))


In this page (written and validated by ) you learned about Scala Annotations . What's Next? If you are interested in completing Scala tutorial, your next topic will be learning about: Scala Best Practices.

Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue:

Share On:

Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using, you agree to have read and accepted our terms of use, cookies and privacy policy.