Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7278063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T22:49:27+00:00 2026-05-28T22:49:27+00:00

EDIT Leaving this for posterity, but nearly a year later, to get down voted,

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T22:49:28+00:00Added an answer on May 28, 2026 at 10:49 pm

    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, T is a type parameter, bounded above by the type _Team (which I assume is either a class or a trait). You subsequently ignore T, 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 RosterController into a _Team because it either is one or it isn’t. I can infer this because RosterController must be a concrete class as you are instantiating it. It’s either a subtype of _Team or it is not.

    If _Team is a trait, it can be mixed in:

    new RosterController with _Team
    

    But because you don’t explain what you are trying to actually do, it’s difficult to say for sure! Is RosterController also 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 match statement.

    Here’s a useful example as to how the compiler can infer really quite a lot of cool stuff

    scala> class Bar; class Baz; trait Foo
    defined class Bar
    defined class Baz
    defined trait Baz
    

    Now let’s create a match expression:

    scala> def foo(s: String) = s match {
       | case "a" => new Baz with Foo
       | case _   => new Bar with Foo
       | }
    foo: (s: String)ScalaObject with Foo
    

    So the compiler has correctly inferred Foo here. Perhaps you could rephrase your question, distilling in a REPL example exactly what you expect and how that differs from what actually happens.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's hope I can dumb this down without leaving out crucial details... I've got
[EDIT: further digging revealed a different root issue. I'm rephrasing the question, but leaving
EDIT: changed the title. I didn't know it at the time but this is
(Leaving aside hair-splitting about if this is integration-testing or unit-testing.) I would rather first
Im nearly finished with this website but for one problem. On iphones (and possibly
I asked this on ServerFault, but didn't get any reply's, I know it's command
EDIT 07/14 As Bill Burgess mentionned in a comment of his answer, this question
Edit (updated question) I have a simple C program: // it is not important
EDIT : It turned out that this can only be done through an external
Edit: To be clear, please understand that I am not using Regex to parse

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.