Open In App

How to find instance of a value type in Scala?

Last Updated : 10 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will learn how to find an instance of a value type in Scala. “Instance of a value” refers to checking whether a variable holds a specific type of data or object in a programming language.

Finding Instance of a Value Type in Scala

Below are the possible approaches to finding instances of a value type in Scala.

Approach 1: Using Pattern Matching

  1. In this approach, we are using pattern matching in Scala, denoted by the match keyword followed by cases with type patterns such as _: Int, _: Double, etc., to check the instance of the value against different types.
  2. Each case handles a specific type, and the underscore _ serves as a wildcard for any value.
  3. If the value matches any of the specified types, the corresponding case is executed, printing a message indicating the type of the value.
  4. If none of the cases match, the default case _ => println(“Unknown value type”) is executed.

In the below example, pattern matching is used to find an instance of a value type in Scala.

Scala
// Creating Object
object GFG {
// Main Method
  def main(args: Array[String]): Unit = {
    // Defining Value
    val value: Any = 42
    // Findinf Instance of Value Type
    value match {
      case _: Int     => println("Value is an Integer")
      case _: Double  => println("Value is a Double")
      case _: Boolean => println("Value is a Boolean")
      case _: String  => println("Value is a String")
      case _          => println("Unknown value type")
    }
  }
}

Output:

Screenshot-2024-03-28-at-09-12-23-Scastie---An-interactive-playground-for-Scala

Approach 2: Using isInstanceOf

  1. In this approach, we are using the isInstanceOf method in Scala, which checks if an object is an instance of a specified class or its subclasses.
  2. The code checks the instance of the value against different types using isInstanceOf[Int], isInstanceOf[Double], etc., and prints a message indicating the type of the value based on the result of these checks.
  3. If none of the checks match, it prints “Unknown value type”.

In the below example, isInstanceOf is used to find an instance of a value type in Scala.

Scala
// Creating Object
object GFG {
// Main Method
  def main(args: Array[String]): Unit = {
    // Defining Value
    val value: Any = 3.14
    // Finding Instance of Value Type
    if (value.isInstanceOf[Int]) {
      println("Value is an Integer")
    } else if (value.isInstanceOf[Double]) {
      println("Value is a Double")
    } else if (value.isInstanceOf[Boolean]) {
      println("Value is a Boolean")
    } else if (value.isInstanceOf[String]) {
      println("Value is a String")
    } else {
      println("Unknown value type")
    }
  }
}

Output:

Screenshot-2024-03-28-at-09-14-11-Scastie---An-interactive-playground-for-Scala

Approach 3: Using match

  1. In this approach, we are using the match expression in Scala, which allows us to match a value against a set of patterns.
  2. The code defines a value value of type Any and uses pattern matching to check its instance against different types (Int, Double, Boolean, String).
  3. Depending on the matched type, it assigns a corresponding string to valueType. If none of the patterns match, it assigns “Unknown” to valueType.
  4. Finally, it prints a message indicating the type of the value based on the result of the match.

In the below example, match is used to find an instance of a value type in Scala.

Scala
// Creating Object
object GFG {
// Main Method
  def main(args: Array[String]): Unit = {
    // Defining Value
    val value: Any = true
    // Findig Instance of Value Type
    val valueType = value match {
      case _: Int     => "Integer"
      case _: Double  => "Double"
      case _: Boolean => "Boolean"
      case _: String  => "String"
      case _          => "Unknown"
    }
    println(s"Value is a $valueType")
  }
}

Output:

Screenshot-2024-03-28-at-09-15-47-Scastie---An-interactive-playground-for-Scala



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads