Introduction to Scala Either
Scala either is used to represent one of the two possible variables. It takes two parameters or we can say it has two child. These two child are described as left and right. Also, scala either is the alternative to scala Option. Also, it is used to deal with missing values. For more understanding, we can say that right is for success and left is for failure. Here left is for failure that means by using it we can return the error if any of the errors occurred inside the left child of either in scala.
Syntax:
Either[data_type, data_type]
Fo using it in our program we have a very simple syntax for this. We use Either keyword with two possible values in it as a left and right child for success and failure. We can pass any type of data type as left and right inside the either in scala. 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?
Scala either we can say is the alternative to the Option in scala. It has two child left and right which stand for failure and success respectively. This is present inside the scala.util package. Also, the left and right instance of either can be an instance of scala.util.Right or scala.util.Left, but these two instances have replaced two things. scala. Some is replaced by scala.util.Right and scala. None is replaced by scala.util.Left. By using this we can determine that the instance is of the left or right type in scala. This scala either can also be used with comprehensions means with the combination of 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("")
}
4.5 (5,543 ratings)
View Course
In this example, we are creating one Demo function which is validating that the passing parameter is empty or not by using the right and left of either in scala. If the parameter passed is not empty we are returning the value that we have passed which means a success. If the parameter passed does not contain any value that means it is empty then we are returning Left with one error message that stands for failure in either of scala.
This contains both abstract and concrete members which are as follows:
- isLeft: This method return a Boolean value true or false. If the instance is left then it returns true else false.
- isRight: This method return a Boolean value true or false. If the instance is Right then it returns true else false.
- productArity: This method returns an Integer value. Basically it returns the size if the product.
Examples to Implement Scala Either
Below are the examples:
Example #1
In this example we are simply using either to check the value passed using its child left and right in scala.
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 the instances as 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 are checking the student record is found or not by using the right and left child of Either in scala.
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
Points need to be remembering while working with either in scala:
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] then you have to 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 type mismatch error.
5. All these Either, Right, Left are present inside the same package of scala that is Scala.util.
6. It contains concrete vale members as well.
Conclusion
So scala either is the best choice if we want to work with two instances and check between them. It is very similar to the Option available in scala. We can return two things by using the either in scala. One is a success by using the Right() instance another one is a failure by using the Left() instance. It should match with the instance that we are passing inside Either otherwise it will give type mismatch error in scala.
Recommended Articles
This is a guide to Scala Either. Here we discuss a brief overview on Scala Either and its Syntax along with its different examples and code Implementation. You can also go through our other suggested articles to learn more –