I am attempting to refactor a function (found towards the end of this StackOverflow answer) to make it slightly more generic. Here's the original function definition:
def tryProcessSource(
file: File,
parseLine: (Int, String) => Option[List[String]] =
(index, unparsedLine) => Some(List(unparsedLine)),
filterLine: (Int, List[String]) => Option[Boolean] =
(index, parsedValues) => Some(true),
retainValues: (Int, List[String]) => Option[List[String]] =
(index, parsedValues) => Some(parsedValues)
): Try[List[List[String]]] = {
???
}
And here is to what I am attempting to change it:
def tryProcessSource[A <: Any](
file: File,
parseLine: (Int, String) => Option[List[String]] =
(index, unparsedLine) => Some(List(unparsedLine)),
filterLine: (Int, List[String]) => Option[Boolean] =
(index, parsedValues) => Some(true),
transformLine: (Int, List[String]) => Option[A] =
(index, parsedValues) => Some(parsedValues)
): Try[List[A]] = {
???
}
I keep getting a highlight error in the IntelliJ editor on Some(parsedValues)
which says, "Expression of type Some[List[String]] doesn't conform to expected type Option[A]". I've been unable to figure out how to properly modify the function definition to satisfy the required condition; i.e. so the error goes away.
If I change transformLine
to this (replace the generic parameter A
with Any
)...
transformLine: (Int, List[String]) => Option[Any] =
(index, parsedValues) => Some(parsedValues)
...the error goes away. But, I also lose the strong typing associated with the generic parameter.
Any assistance on with this is very much appreciated.