Updated May 15, 2023
Introduction to Scala Either
Scala either is used to represent one of the two possible variables. It takes two parameters, or it has two children. These two children are described as left and right. Also, Either is the alternative to the Scala Option. Also, it is used to deal with missing values. For more understanding, right is for success, and left is for failure. Here left is for failure, which means by using it, we can return the error if any of the mistakes occurred inside the left child of either in Scala.
Syntax:
Either[data_type, data_type]
Fo using it in our program, we have a straightforward syntax for this. We use Either keyword with two possible values as a left and right child for success and failure. We can pass any data type as left and right inside either. Let’s see one practice syntax for a better understanding of it.
Either[String, String]
In this simple way, we can define it.
How Does Either Work in Scala?
Either we can say is the alternative to the Option. It has two children, left and right, representing failure and success, respectively. This is present inside the scala—util package. Also, the left and right instances of either can be an instance of scala. Util.Right or Scala. Util. Left, but these two instances have replaced two things. Scala. Scala replaces some. Util.Right, and Scala. Scala replaces none. Util.Left. Using this, we can determine whether the instance is of the left or suitable type. This scale can also be used with comprehension means combining For loop and yield.
Now we can have its extended class, supertypes, and some of the known sub-classes:
1. extended class available in scala:
- Product
- Serializable
2. Supertypes available in scala:
- Serializable
- io.Serializable
- Product
- Equals
- AnyRef
- Any
3. Known Subclasses available in scala:
- Left
- Right
Now take one example for beginners to understand the working with a program:
object Main extends App{
// Your code here
defDemo(name: String): Either[String, String] =
{
if (!name.isEmpty)
Right(name)
else
Left("Empty String not passed!! ")
}
println(Demo(" "))
println(Demo("Some string we are passing here"))
println("")
}
In this example, we create a Demo function to validate whether the passing parameter is empty using the right and left of Either. If the passed parameter is not empty, we return the passed value, indicating success. If the parameter passed does not contain any value, it is empty; then we are returning Left with one error message that stands for failure in either scale.
This contains both abstract and concrete members,, which are as follows:
- isLeft: This method returns a Boolean value, true or false. If the instance is left,, then it returns true else, false.
- is right: This method returns a Boolean value, true or false. If the instance is Right, then it returns true else, false.
- productivity: This method returns an Integer value. It returns the size of the product.
Examples to Implement Either
Below are the examples:
Example #1
In this example,, we use either to check the value passed using its child left and right.
Code:
object Main extends App{
// Your code here!
//creating function using either
defDemo(name: String): Either[String, String] =
{
// right instance here
if (!name.isEmpty)
Right(name)
else
// left instance here
Left("Empty String passed!! ")
}
//calling method to check the output.
println(Demo(" "))
println(Demo("Some string we are passing here"))
println(Demo(""))
}
Output:
Example #2
In this example, we are checking number is divisible by 2 or not. Making both instances an integer only.
Code:
object Main extends App{
// Your code here!
//creating function using either
defCheckNumber(num: Int): Either[Int, Int] =
{
// right instance here
if (num % 2 == 0 ) {
println("Number is divisible by 2 !!")
Right(num)
}
else{
// left instance here
println("Number is not divisible by 2 !!")
Left(num)
}
}
//calling method to check the output.
println(CheckNumber(20))
println(CheckNumber(50))
println(CheckNumber(3))
}
Output:
Example #3
In this example, we use the right and left child of Either to check whether the student record is found or not.
Code:
object Main extends App{
// Your code here!
//creating function using either
defCheckRecord(num: Int, name: String): Either[String, String] =
{
// right instance here
if (num == 001 ) {
println("Id is 0001 for student")
println("is is :: "+ num)
Right(name)
}
else{
// left instance here
println("Id is not 0001 for student")
println("is is :: "+ num)
Left(name)
}
}
//calling method to check the output.
println(CheckRecord(002, "student 1 .."))
println(CheckRecord( 001, "student 2 ..."))
println(CheckRecord(003, "student 4....."))
}
Output:
Things to Remember
1. Either has two child right and left
2. Right is for success, and left is for failure
3. Suppose if you pass Either[String, String], you must return the same instance from the right and left. We cannot pass another instance using the right and left, for example:
- Right(“right”)
- Left(“Left”)
4. These both are right, but we cannot return like this: Right(20) or Left9(20). This is wrong and gives a type mismatch error.
5. All these Either, Right, Left are present inside the same package that is Scala.util.
6. It contains concrete vale members as well.
Conclusion
So Scala is the best choice to work with and check between two instances. It is very similar to the Option available. We can return two things by using either. One is a success by using the Right() instance. Another one is a failure by using the Left() instance. It should match the instance that we are passing inside. Either; otherwise, it will give a type mismatch error.
Recommended Articles
We hope that this EDUCBA information on “Scala Either” was beneficial to you. You can view EDUCBA’s recommended articles for more information.