Not as much answer as an explanation of why huynhjl answer is correct ...
Part of your confusion is that you are trying to execute a def partial function. All this is done to create a method that returns a PartialFunction object when you can also directly create an object:
val pf: PartialFunction[SomeType,AnotherType] = { case s1:SubType1 => AnotherType(s1.whatever) case s2:SubType2 => AnotherType(s2.whatever) }
Although I personally prefer to use titration:
val pf = { case s1:SubType1 => AnotherType(s1.whatever) case s2:SubType2 => AnotherType(s2.whatever) } : PartialFunction[SomeType,AnotherType]
In any case, you need to specify the input type, so you need to specify the exact signature of the PartialFunction . I know that it seems that it should be possible to do this, but, alas, this, unfortunately, is not so!
Using the given version, you can then define and raise everything in the same place:
val pf = ({ case s1:SubType1 => AnotherType(s1.whatever) case s2:SubType2 => AnotherType(s2.whatever) } : PartialFunction[SomeType,AnotherType]).lift
PartialFunction.condOpt is a better solution, though, since it allows the specialist to do most of this work for you, leaving much cleaner code :)
source share