EDIT
Leaving this for posterity, but nearly a year later, to get down voted, nice…would not do anything like the below now, but at the time, getting started with Scala, questions generally come from a place of ignorance….
ORIGINAL
I know I can just wrap a pattern match in a method with generic type specified like so:
def getTeam[T <: _Team](clazz: String): _Team =
clazz match {
case "roster" => new RosterController
case "schedule" => new ScheduleController
}
and get a meaningful type (_Team) for the compiler to work with.
However, I am wondering if it is possible to do this WITHOUT a wrapper method and without asInstanceOf[_Team] boilerplate? i.e. something other than
clazz match {
case "roster" => new RosterController.asInstanceOf[_Team]
case "schedule" => new ScheduleController.asInstanceOf[_Team]
}
Not the end of the world if not possible, but would prefer to do the matches in place vs. splitting into a separate method.
Thanks
I’m not sure what you think is going on in this method but it seems like you are either confused or have written the wrong thing down.
In your example,
Tis a type parameter, bounded above by the type_Team(which I assume is either a class or a trait). You subsequently ignoreT, so it could be a phantom type but from your question, but it doesn’t really seem like it.You cannot possibly need to cast
RosterControllerinto a_Teambecause it either is one or it isn’t. I can infer this becauseRosterControllermust be a concrete class as you are instantiating it. It’s either a subtype of_Teamor it is not.If
_Teamis a trait, it can be mixed in:But because you don’t explain what you are trying to actually do, it’s difficult to say for sure! Is
RosterControlleralso parameterized?EDIT – from the comments below, it seems that you are wondering about what the compiler infers as the result type of an expression where that expression is a
matchstatement.Here’s a useful example as to how the compiler can infer really quite a lot of cool stuff
Now let’s create a match expression:
So the compiler has correctly inferred
Foohere. Perhaps you could rephrase your question, distilling in a REPL example exactly what you expect and how that differs from what actually happens.