Scala Annotations

Scala | Annotations: Here, we will learn about annotations in Scala. And learn different types of annotations and how they are used in the program.
Submitted by Shivang Yadav, on December 29, 2020

Annotations are the extra information that is added to the document. In Scala, it can be defined as metadata that is added to definitions in the program. It can be added to the definition and declaration of vals, vars, classes, objects, traits, defs, and types to provide some meta-information to the compiler about these.

Syntax:

The annotations in Scala are declared using @annot

@annot(expression_{1}, expression_(2)....) {val valName_{1} = const_{1} , ...}

There are some predefined annotations that are defined in Scala to support users working with them, some are inherited from and some are added new in Scala.

Java Annotations

These are directly inherited from the java platform and used in scala.

  • @transient: This annotation is used to mark a field as transient or non-persistent.
  • @volatile: This annotation is used to mark a field as volatile i.e. it can change its value even outside the program's control.
  • @serialVersionUID(): This annotation is used to add a serial version id to the given class, the id is a constant of log int type.

Java Beans Annotations:

These annotations are taken from java beans and are included as scala.beans.

  • @scala.beans.BeanProperty: This annotation is used to add the javabean style getter and setter methods to the parent class of the variable. It is used as a prefix to the definition of a variable and adds the getX and setX method to the class of the variable.
  • @scala.beans.BooleanBeanProperty: This annotation is used to add the getter method to the class. The difference between both the annotations is the former one adds the getX getter and the later one adds isX getter to the class of the variable.

Deprecation Annotations:

  • @deprecated(message: , since): This annotation is with a definition of a function, class, etc and it marks the definition as deprecated. By doing this all the deprecation warnings are suppressed in the code.

Syntax:

@deprecated("message" , "since")

Program:

// Program to illustrate deprecated annotations in scala 

object myObject{
def main(args:Array[String]) { 
        println("Hello! Before deprecated message ")
        @deprecated("This is a deprecated Message!", "release # which deprecates method")
        def value = "Hello! Before deprecated message"
        value
    }
}

Output:

Hello! Before deprecated message 

warning: there was one deprecation warning (since release # which deprecates method)

The annotation clause applies to the first definition or declaration following it. Like here the statement after the deprecated annotation is deprecated.

Scala Compiler Annotations:

These are annotations for the compiler added in the Scala.

  • @uncheckedStable: This annotation is used to allow the value to which it is added to appear in the path even though the type of the value is volatile for the code block.
    Syntax:
    @annotations.unchecked.uncheckedStable val x : A with B = null
    
  • @tailrec: This annotation is used to tell the compiler that this method is tail recursive and to allot some extra memory to avoid issues.
    Syntax:
    @tailrec
    

Program to show implementation of tailrec annotation in Scala

import scala.annotation.tailrec

object myObject { 
    def calcFactorial(x: Int): Int = {
        @tailrec
        def calcFactorialRec(x: Int, accumulator: Int): Int = {
            if (x == 1) accumulator else calcFactorialRec(x - 1, accumulator * x)
        }
        calcFactorialRec(x, 1)
    }
    def main(args:Array[String]) 
    { 
        println( "The factorial of 5 is " + calcFactorial(5) )
    }
}

Output:

The factorial of 5 is 120



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.