Skip to content

Instantly share code, notes, and snippets.

@rokon12
Created October 4, 2015 16:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rokon12/c4be63a90f5f8b0134c5 to your computer and use it in GitHub Desktop.
Save rokon12/c4be63a90f5f8b0134c5 to your computer and use it in GitHub Desktop.
Example of Either in Scala
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