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 6839485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:43:28+00:00 2026-05-26T23:43:28+00:00

Is it possible to create a Class object purely from a generic parameter? For

  • 0

Is it possible to create a Class object purely from a generic parameter? For example:

class myclass[T] { 
  def something(): Class[_ <: T] = 
    classOf[T] //this doesn't work
}

Since the type will have been erased at runtime, it seems like this a job for manifests, but I haven’t found an example that demonstrates this particular usage. I tried the following, but it doesn’t work either:

class myclass[T] { 
  def something()(implicit m: Manifest[T]): Class[_ <: T] = 
    m.erasure //this doesn't work
}

I suspect this failure is due to, as the API points out, there is no subtype relationship between the type of m.erasure‘s result and T.

EDIT: I’m not really interested in what the type T is, I just need an object of type Class[_ <: T] to pass to a method in the hadoop framework.

Any pointers?

  • 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-26T23:43:28+00:00Added an answer on May 26, 2026 at 11:43 pm

    You can cast the result of m.erasure to a Class[T]:

    class myclass[T] { 
        def something()(implicit m: Manifest[T]): Class[T] = 
            m.erasure.asInstanceOf[Class[T]]
    }
    

    This works fine for basic (non-generic) types:

    scala> new myclass[String]().something()
    res5: Class[String] = class java.lang.String
    

    But note what happens if I use an instantiated type constructor like List[String] for T:

    scala> new myclass[List[String]]().something()
    res6: Class[List[String]] = class scala.collection.immutable.List
    

    Due to erasure, there is only one Class object for all the possible instantiations of a given type constructor.

    Edit

    I’m not sure why Manifest[T].erasure returns Class[_] instead of Class[T], but if I had to speculate, I would say it’s to discourage you from using the methods on Class which allow you to compare two classes for equality or a subtype relationship, since those methods will give you wrong answers when the Class is parameterized with an instantiated generic type.

    For example,

    scala> classOf[List[String]] == classOf[List[Int]]
    res25: Boolean = true
    
    scala> classOf[List[String]].isAssignableFrom(classOf[List[Int]])
    res26: Boolean = true
    

    These results might surprise you and/or lead to a bug in your program. Instead of comparing classes this way, you should normally just pass around Manifests instead and compare them, since they have more information*:

    scala> manifest[List[String]] == manifest[List[Int]]
    res27: Boolean = false
    
    scala> manifest[List[String]] >:> manifest[List[Int]]
    res28: Boolean = false
    

    As I understand it, Manifests are meant to supersede Classes for most use cases… but of course, if you’re using a framework that requires a Class, there’s not much choice. I would suppose that the imposition of casting the result of erasure is just a sort of "acknowledgement of liability" that you’re using an inferior product at your own risk 🙂

    * Note that, as the documentation for Manifest says, these manifest comparison operators "should be considered approximations only, as there are numerous aspects of type conformance which are not yet adequately represented in manifests."

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

Sidebar

Related Questions

Is it possible to create an object from a class with a dynamic name?
Sorry if this has been asked before. Is it possible to create class in
I create class Widget, it creates window, this class paints something on the window
Possible Duplicate: Create new class from a Variable in Java I have a string
I wonder, whether it is possible to create class-methods in VBA. By class-method I
In Java, is it possible to create a class definition on the fly while
Is it possible to create an inner class within an interface? If it is
I know it's possible to create a friend function in C++: class box {
I was wondering if the following is possible. Create a class that accepts an
By any means, is it possible to create an instance of a PHP class

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.