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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:14:46+00:00 2026-06-13T09:14:46+00:00

In Scala, I need to execute code that fulfills the following requirement: Pass a

  • 0

In Scala, I need to execute code that fulfills the following requirement:

Pass a java.lang.Class into a factory method. The class passed to this method must extend the abstract class Project. I will instantiate this class, perform various actions, and return it.

Here’s what I have…

object ProjectFactory {

    def create (clazz: java.lang.Class[T <: Project]): Project = {
        val newProject = clazz.newInstance
        /* snip */
        newProject
    }

}

abstract class Project

…and here is the error I am getting:

identifier expected but <: found

I know this is just a syntax issue – how do I express this logic?

  • 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-13T09:14:47+00:00Added an answer on June 13, 2026 at 9:14 am

    You need to declare the type parameter after the method name, not at the use site:

    object ProjectFactory {
    
        def create[T <: Project](clazz: java.lang.Class[T]): Project = {
            val newProject = clazz.newInstance
            /* snip */
            newProject
        }
    
    }
    
    abstract class Project
    

    I’m not an expert with Scala’s reflection libraries, but I think this might be cleaner:

    object ProjectFactory {
    
        def create[T <: Project: Manifest]: Project = {
            val newProject = classManifest[T].erasure.newInstance.asInstanceOf[Project]
            /* snip */
            newProject
        }
    
    }
    
    abstract class Project
    

    That way you don’t have to explicitly pass in a class object—you just parameterize the method with the type you want. Again, I bet there’s a better way to do this, so I’d appreciate feedback from those with more experience in this area.

    Also, I think Scala 2.10 has dropped Manifests in favor of ClassTags or something like that…

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

Sidebar

Related Questions

I need to convert the following java method to scala and am having difficulty
I have a Scala/Java dual language project where I need to pass a Scala
My goal is to enhance inside scala code an existing Java class using a
I need to print a formatted string containing scala.Long. java.lang.String.format() is incompatible with scala.Long
I have the following spring pseudo-code wired scala class. @Service class Something { @Value(${some.property})
I'm new to scala and need clarification on the following code snippet involving constructors
I am a Scala newbie and I need to port part of a java
What is a standard way of profiling Scala method calls? What I need are
For an open-source multiplayer programming game written in Scala that loads players' bot code
I need to do a pattern in Scala, this is a code: object Wykonaj{

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.