Created
October 4, 2015 16:48
-
-
Save rokon12/c4be63a90f5f8b0134c5 to your computer and use it in GitHub Desktop.
Example of Either in Scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.net.URL | |
import scala.io.Source | |
def getContent(url: URL): Either[String, Source] = | |
if (url.getHost.contains("google")) | |
Left("Requested URL is blocked for the good of the people!") | |
else | |
Right(Source.fromURL(url)) | |
getContent(new URL("http://bazlur.com")) match { | |
case Left(msg) => println(msg) | |
case Right(source) => source.getLines().foreach(println) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment