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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:56:37+00:00 2026-06-13T22:56:37+00:00

I have a class such as this: class NormalClass[T <: NormalClass[T]] { object One

  • 0

I have a class such as this:

class NormalClass[T <: NormalClass[T]] {
  object One
  object Two
}

and I wish to be able to create a new instance of the above class in a typed trait. The following def make in MetaClass creates an instance of T but it lacks the internal objects associated with NormalClass.

trait MetaClass[T <: NormalClass[T]] {
  def make:T = this.getClass.getSuperclass.newInstance.asInstanceOf[T]
} 

I have two questions, what is the reason for the missing objects and what is the best way, using reflection, to initiate a new class with internal objects from its type

EDIT: More Detail

The problem I am facing is if I then create an instance using make e.g. var f = make and I try to access and object method e.g. f.One.getSomething I get the error value One is not a member of type parameter T.

  • 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-13T22:56:38+00:00Added an answer on June 13, 2026 at 10:56 pm

    So I think your problem in particular is the reflection:

    this.getClass.getSuperclass.newInstance.asInstanceOf[T]
    

    Here, this is your instance of MetaClass, and there’s no particular reason to believe that the superclass of this is the class you want to instantiate. For example:

    class Foo extends NormalClass[Foo]
    object Foo extends MetaClass[Foo]
    

    In this case, the superclass of the object Foo is not a NormalClass at all, it’s java.lang.Object. As a result, it won’t have members like One or Two, and you’ll get a ClassCastException if you try to cast it to T.

    If you want the make method to instantiate an object of type T, then you need to get the runtime class of T, and then use that to create the new instance. You can accomplish this by implicitly acquiring a ClassTag:

    class NormalClass[T <: NormalClass[T]] {
      object One
      object Two
    }
    trait MetaClass[T <: NormalClass[T]] {
      def make(implicit classTag: scala.reflect.ClassTag[T]): T =
        classTag.runtimeClass.newInstance.asInstanceOf[T]
    }
    
    // declare a class and an object for creating instances of that class
    class Foo extends NormalClass[Foo]
    object Foo extends MetaClass[Foo]
    
    // create a new instance of Foo and access its fields
    val foo = Foo.make
    foo.One
    foo.Two
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have a PHP class such as this one: class A { public
I have a class that represents some business object, such as this: Public Class
I have a Pic class with methods such as this.pic_w, this.pic_h. Inside one of
I have a class that gets arranged into a hierarchy, such as this: class
This is a client part of programm. I have such class which works with
I have a class that gets arranged into a hierarchy, such as this: class
I have a class Data which implements Serializable interface. This class has such fields
I'm experementing with Jackson serialization/deserialization. For instance, I have such class: class Base{ String
If I have a definition in a class header such as this: vector<baddie*> baddies;
I have a tree of lists in Html such as this: <ul class=myTree> <li

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.