Is it possible to write a method in Scala which returns an object of a type-parameterized class with different type paramter ? Something like this:
class A[T]
def f(switch: Boolean): A = if(switch) new A[Int] else new A[String]
Please note: The Code above is fictional to show the type of problem; The code above does not make semantically sense.
The code above will not compile because return type A is not parameterized.
You can, and you can even do it with type-safety with the aid of implicit arguments that encapsulate the pairings:
You do have to explicitly map from boolean values to the custom
TrueandFalsevalues.(I have chosen
Listas the target class just as an example; you could pick anything or even make it generic with a little more work.)(Edit: as oxbow_lakes points out, if you need all possible return values to be represented on the same code path, then this alone won’t do it, because the superclass of
List[Int]andList[String]isList[Any], which isn’t much help. In that case, you should use anEither. My solution is for a single function that will be used only in theTrueorFalsecontexts, and can maintain the type information there.)