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

  • SEARCH
  • Home
  • 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 6378155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:58:06+00:00 2026-05-25T01:58:06+00:00

Is it possible to get the type name of a generic class in Scala?

  • 0

Is it possible to get the type name of a generic class in Scala? I know this isn’t possible in Java with type erasure, but I was hoping that Scala would be a different case.

Currently I have to do something similar to this:

trait Model
case class User(id: String) extends Model

def fromMap[M<:Model : Manifest](data: Map[String, String], modelType: String) = {
  modelType match {
    case "user" => User(data.get("id").get)
  }
}

val user = fromMap[User](Map("id" -> "id1"), "user")

Obviously it would be easier if I could work out “user” without having to have it passed in.

  • 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-25T01:58:07+00:00Added an answer on May 25, 2026 at 1:58 am

    Class name can be retrieved from a Manifest (or a ClassManifest) with manifest.erasure.getName (erasure is the Class instance). For instance

    def className[A : ClassManifest] = classManifest[A].erasure.getName
    

    Edit : Seen Derek’s answer, which makes this stuff with erasure.getName look rather stupid. I didn’t consider toString. Still I hope what follows may be of interest

    The difference between using ClassManifest and Manifest is that in the Manifest for a generic class, the type parameter are guaranteed to be available, while they are best effort in ClassManifest (compare signatures of typeParameters in both classes). The drawback of this guarantee is that a Manifest might not be implicitly available where a ClassManifest would be.

    Have you considered using typeclasses instead?

    trait BuilderFromMap[A] {def build(data: Map[String, String]): A} 
      // or better, return Option[A], accounting for possible failure
    object User {
       implicit val Builder extends BuilderFromMap[User] {...}
    }
    def fromMap[A](data: Map[String, String])(implicit builder: BuilderFromMap[A])
      = builder.build(data)
    

    This way, calls to fromMap will compile only if a Builder is available for this particular class, rather than fail with a MatchError.

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

Sidebar

Related Questions

Possible Duplicate: PHP detecting request type (GET, POST, PUT or DELETE) This should be
Possible Duplicate: Get variable name. javascript “reflection” Is there a way to know the
It probably isn't possible, but I want to check if something like this can
Is it possible to reflectively instantiate a generic type in Java? Using the technique
Is it possible in JSP to get the type of Object in List, just
Possible Duplicate: Default value of a type In C#, to get the default value
does anyone know how to (if possible) get the owner of the tag on
Possible Duplicate: Get file name from URI string in C# How to extract file
In Java, renaming threads is possible. In .NET it is not. This is because
As you all know creating a web service with generic methods is not possible.

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.