I’m trying to generalize the following code:
def fetchUrl = {
try {
val lineList = Source.fromURL(url).getLines.toList
process(lineList)
}
catch {
case ex: java.net.UnknownHostException => ex.printStackTrace()
}
}
I want to be able to fetch URL’s (fromURL) and files (fromFile) with the same method. Is it possible to generalize this code to archive this?
I figured I could use pattern matching for this but I don’t know how.
def fetchSource(src: Source, str: String) = src match {
case ??? => Source.fromURL(url).getLines.toList
case ??? => Source.fromFile(str).getLines.toList
}
Is there a way to get this right?
Thanks in advance.
The simplest solution is to have one method that will fetch a given
Source, and two wrapper methods that buildSourcefromFileorURL.