Home » Scala language

Scala Methods to Call Options

In this tutorial on methods to call options in Scala, we will see different methods in Scala programming language that can be used to call an option in Scala.
Submitted by Shivang Yadav, on March 08, 2020

Option in Scala is used when the method's return type is not fixed. It might be a value or it can be a NULL object in Scala. In Scala, it can be referred to as a carrier object that can contain single or no elements.

In the Scala programming language, there can be a few methods that can be used to call the Scala option.

get Method

The get method in Scala returns an Option's value. This method does not return a NULL value and returns an Error instead.

Program:

object myObject 
{ 
	def main(args: Array[String]) 
	{ 
		val opt:Option[String] = Some("Includehelp") 
		val value = opt.get 
		println(value) 
	} 
} 

Output

Includehelp

productArity Method

The productArity method returns an integer value which is the size of the option's value.

Program:

object myObject 
{ 
	def main(args: Array[String]) 
	{ 
		val opt:Option[String] = Some("Includehelp") 
		val value = opt.productArity 
		println(value) 
	} 
} 

Output

1

productElement(n:Int) Method

The productElement() method in Scala is used to return the nth element of a product which is 0-indexed.

Program:

object myObject 
{ 
	def main(args: Array[String]) 
	{ 
		val opt:Option[Int] = Some(76) 
		val value = opt.productElement(0) 
		println(value) 
	} 
} 

Output

76

exists Method

This method of Scala programming language is used to check if the option value satisfies a given condition or not. And returns a boolean value based on it.

Program:

object myObject 
{ 
	def main(args: Array[String]) 
	{ 
		val opt:Option[Int] = Some(76) 
		val value = opt.exists(x =>{ x % 2 == 0 }) 
		if(value){
		    print("Value is even");
		}
		else
		    print("Value is odd");
	} 
} 

Output

Value is even

filter() Method

The filter() method in Scala is used to return the value of Option. This value is returned only when the stated condition is satisfied.

Program:

object myObject 
{ 
	def main(args: Array[String]) 
	{ 
		val opt:Option[Int] = Some(76) 
		val value = opt.filter(x =>{ x % 2 == 0 }) 
		print(value)
	} 
}

Output

Some(76)

filterNot() Method

Just opposite of the filter() method, the filterNot() method returns the value of the option but only when the condition is not satisfied.

Program:

object myObject 
{ 
	def main(args: Array[String]) 
	{ 
		val opt:Option[Int] = Some(7) 
		val value = opt.filterNot(x =>{ x % 2!= 0 }) 
		print(value)
	} 
}

Output

None

isDefined Method

The isDefined method in Scala is used to return boolean value based on the instance of option i.e. true if some value is present and false when none is present.

Program:

object myObject 
{ 
	def main(args: Array[String]) 
	{ 
		val opt:Option[Int] = Some(7) 
		val value = opt.isDefined 
		print(value)
	} 
} 

Output

true


Comments and Discussions!

Load comments ↻





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