In Scala Either, functions exactly similar to an Option. The only dissimilarity is that with Either it is practicable to return a string which can explicate the instructions about the error that appeared. The Either has two children which are named as Right and Left where, Right is similar to the Some class and Left is same as None class. Left is utilized for the failure where, we can return the error occurred inside the child Left of the Either and Right is utilized for Success.
Example:
Either[String, Int]
Here, the String is utilized for the Left child of Either as its the left argument of an Either and Int is utilized for the Right child as its the right argument of an Either. Now, let’s discuss it in details with the help of some examples.
-
Example :
// Scala program of Either// Creating object and inheriting// main method of the trait AppobjectGfGextendsApp{// Defining a method and applying// EitherdefName(name:String):Either[String, String]={if(name.isEmpty)// Left child for failureLeft("There is no name.")else// Right child for successRight(name)}// Displays this if name is// not emptyprintln(Name("GeeksforGeeks"))// Displays the String present// in the Left childprintln(Name(""))}chevron_rightfilter_noneOutput:Right(GeeksforGeeks) Left(There is no name.)
Here, isEmpty method checks if the field of name is empty or filled, if its empty then Left child will return the String inside itself and if this field is not empty then the Right child will return the name stated.
-
Example :
// Scala program of Either with// Pattern matching// Creating object and inheriting// main method of the trait AppobjecteitherextendsApp{// Defining a method and applying// EitherdefDivision(q:Int, r:Int):Either[String, Int]={if(q==0)// Left child for failureLeft("Division not possible.")else// Right child for successRight(q / r)}// Assigning valuesvalx=Division(4,2)// Applying pattern matchingxmatch{caseLeft(l)=>// Displays this if the division// is not possibleprintln("Left: "+ l)caseRight(r)=>// Displays this if division// is possibleprintln("Right: "+ r)}}chevron_rightfilter_noneOutput:Right: 2
Here, the division is possible which implies success so, Right returns 2. Here, we have utilized Pattern Matching in this example of Either.
Recommended Posts:
- Scala Tutorial – Learn Scala with Step By Step Guide
- Scala short <(x: Short): Boolean
- Scala short <(x: Char): Boolean
- Scala Extractors
- Scala | Partially Applied functions
- Scala String indexOf(String str) method with example
- Scala String contentEquals() method with example
- Scala Keywords
- Scala Int /(x: Int) method with example
- Scala Int /(x: Short) method with example
- Program to print Java Set of characters in Scala
- Scala Map size() method with example
- Scala SortedMap addString() method with a start, a separator and an end with example
- Scala Iterator addString() method with example
- Scala String substring(int beginIndex, int endIndex) method with example
- Scala | Functions Call-by-Name
- Program to convert Java list to an iterator in Scala
- Scala Set &() method with example
- Scala | Type Inference
- Program to convert Java set of Shorts to an Indexed Sequence in Scala
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

