July 2017
Intermediate to advanced
796 pages
18h 55m
English
Either[X, Y] is an instance that contains either an instance of X or an instance of Y but not both. We call these subtypes left and right of Either. Creating an Either is trivial. But it's very powerful sometimes to use it in your program:
package com.chapter3.ScalaFPimport java.net.URLimport scala.io.Sourceobject Either { def getData(dataURL: URL): Either[String, Source] = if (dataURL.getHost.contains("xxx")) Left("Requested URL is blocked or prohibited!") else Right(Source.fromURL(dataURL)) def main(args: Array[String]) { val either1 = getData(new URL("http://www.xxx.com")) println(either1) val either2 = getData(new URL("http://www.google.com")) println(either2) }}
Now, if we pass any arbitrary URL that doesn't contain ...
Read now
Unlock full access