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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:30:12+00:00 2026-05-26T02:30:12+00:00

Basically, I want to have a Map indexed by type objects. In this case,

  • 0

Basically, I want to have a Map indexed by type objects. In this case, I’m trying to use Class as the “type type”.

the following code:

class SuperClass {}
val typemap = new HashMap[Class[_ <: SuperClass], SuperClass]

def insertItem1[T <: SuperClass] (item : T) = typemap += classOf[T] -> item
def insertItem2[T <: SuperClass] (item : T) = typemap += item.getClass -> item

does not compile, because (1) classOf[T] is apparently invalid:

error: class type required but T found

and (2) item.getClass is of the wrong type:

Type mismatch, expected: Class[_ <: SuperClass], actual: Class[_]

I see two ways to accomplish this:
either drop the type requirement and use Class[_] only
or use Manifest instead of Class (not sure how to do that yet, any examples are welcome)

But more generally, why is classOf[T] invalid, and how do I obtain a class object for a parametrized type?

update: I found out that I can use item.getClass.asInstanceOf[Class[T]] to get to the class object. For one, this is ugly. For two, it doesn’t help much, because later in the code i have functions such as this one:

def isPresent[T <: SuperClass] = typemap contains classOf[T]

Obviously, I could use the asInstanceOf method in insertItem and pass the class as a parameter to isPresent. Is there a better way?

  • 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-26T02:30:13+00:00Added an answer on May 26, 2026 at 2:30 am

    You can use manifests to achieve this:

    class TypeMap extends HashMap[Class[_], Any] {
        def putThing[T](t: T)(implicit m: Manifest[T]) = put(m.erasure, t)
    }
    
    val map = new TypeMap
    map.putThing(4)
    map.get(classOf[Int]) // returns Option[Any] = Some(4)
    

    You are losing the types through erasure, so you need to add the implicit argument to the putThing which contains the name of the class, using a Manifest.

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

Sidebar

Related Questions

I have been following this tutorial . Basically I want to take an address
So I basically want to have a static method in my AR class to
This is what I'm talking about: http://www.asp.net/AJAX/AjaxControlToolkit/Samples/ComboBox/ComboBox.aspx basically I want to have a drop
I have a 2-dimensional array of objects and I basically want to databind each
I have this form. Basically what I want is to send a auto-response with
I'm building a pacman game. Basically, I want to have a map representation of
i'm experimenting with django and the builtin admin interface. I basically want to have
Basically I want to have our installer also install the .NET framework and any
I have an SPListItemCollection. I basically want to get one of the items (randomly)
Basically i want to do the below in .NET but i have no idea

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.