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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:09:17+00:00 2026-06-09T13:09:17+00:00

I have a class hierarchy of Item s that each need a corresponding ItemTemplate

  • 0

I have a class hierarchy of Items that each need a corresponding ItemTemplate instance as a constructor parameter. I’d like to write a generic function to instantiate any Item subclass by giving it Item and ItemTemplate as type parameters, using it like this:

val newItem = instantiateItem[ItemClass, TemplateClass](templateInstance)

After a bit of research I now have

def instantiateItem[I, T](implicit mf: Manifest[I], template: T): I = {
    val constructor = mf.erasure.getConstructor(classOf[T])
    constructor.newInstance(template).asInstanceOf[I]
}

But this doesn’t compile, the classOf[T] gives the error

Class type required but T found

I tried replacing classOf[T] with classManifest[CM].erasure but this doesn’t work as CM needs to be context bounded to ClassManifest, and apparently it’s not possible to use bounded type parameters with implicit parameters.

Is it possible to do what I want here?

  • 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-06-09T13:09:19+00:00Added an answer on June 9, 2026 at 1:09 pm

    You can get the template class simply by calling template.getClass. It requires template to be a subtype of AnyRef, so either you cast it to AnyRef (forcing boxing of primitive types) or you add an upper bound for T:

    def instantiateItem[I, T <: AnyRef](implicit mf: Manifest[I], template: T): I = {
      val constructor = mf.erasure.getConstructor(template.getClass)
      constructor.newInstance(template).asInstanceOf[I]
    }
    

    If you want to pass in template explicitly, as indicated by the code in your question, you need to separate implicit and explicit arguments, e.g.

    def instantiateItem[I, T <: AnyRef](template: T)(implicit mf: Manifest[I]): I = {
      val constructor = mf.erasure.getConstructor(template.getClass)
      constructor.newInstance(template).asInstanceOf[I]
    }
    

    aka

    def instantiateItem[I : Manifest, T <: AnyRef](template: T): I = {
      val mf = implicitly[Manifest[I]]
      val constructor = mf.erasure.getConstructor(template.getClass)
      constructor.newInstance(template).asInstanceOf[I]
    }
    

    In general if possible you could avoid having to use reflection at all with careful design:

    trait ItemCompanion[I,T] {
       def instantiateItem(template: T): I
    }
    
    object TestItem extends ItemCompanion[TestItem, TestTemplate] {
       implicit def self: ItemCompanion[TestItem, TestTemplate] = this
       def instantiateItem(template: TestTemplate): TestItem = new TestItem(template)
    }
    class TestItem(template: TestTemplate)
    trait TestTemplate
    
    // try out
    def instantiateItem[I, T](implicit ic: ItemCompanion[I, T], t: T): I =
       ic.instantiateItem(t)
    
    implicit val temp: TestTemplate = new TestTemplate {}
    instantiateItem[TestItem, TestTemplate]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class hierarchy that looks like this: class Base<TElement> { public TElement
I have a simple Class Hierarchy that I am trying to get to work
I have a small class hierarchy where I would like to have a child
I have a class that gets arranged into a hierarchy, such as this: class
I have a class hierarchy like this public abstract class CalendarEventBase{} public class TrainingEvent
I have a collapsible menu item that is defined in XML like this: <item
I have a class that gets arranged into a hierarchy, such as this: class
I have the following class hierarchy. class Header { IEnumerable<Item> Item { get; set;
I have a hierarchy of classes mapped into hibernate that looks kind of like
I have a class hierarchy, each member of which may create IDisposable objects. I

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.