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

  • Home
  • SEARCH
  • 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 4273472
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:43:04+00:00 2026-05-21T07:43:04+00:00

Is there a way I can loop through the set of classes in a

  • 0

Is there a way I can “loop through” the set of classes in a specified package in Scala?

The use case is managing a set of services inheriting from a BaseService trait that get exposed by a provided name to a REST API. A Manager class needs to be able to provide a list of services as well as validate that a provided service exists, and, if so, instantiate it an execute an overloaded function.

My thought is something like this pseudocode:

for ( clazz <- com.demo.pkg ) {
    val service = Class.forName(clazz).newInstance
    registerService( service )
}

Rather than instantiation, static methods on a same-named object to provide service name and description may be better.

In Python, this is trivial because of dir() and in PHP is fairly easy due to the classloader functions but I am new to Scala.

Also, I understand I may be approaching this incorrectly and would welcome feedback.

Update:

I have accepted JPP’s answer below, but he’s correct this is far too expensive a process for a routine operation. So I need to change my approach. The manager class will instead maintain a static list of service clases. While not ideal from a development perspective, the run-time speed gains seem well worth it.

  • 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-21T07:43:04+00:00Added an answer on May 21, 2026 at 7:43 am

    Currently (2.8.1/2.9), Scala has no specific reflection/introspection system, but you’re free to use Java’s. In this particular case, you can port one of the techniques used on the Java side to list all classes in a package, e.g. as shown here (be sure to pick the version in the comments):

    http://snippets.dzone.com/posts/show/4831

    This technique actually doesn’t use Java reflection to find about the classes; what it basically does instead is go through all resources available to the ClassLoader and check which ones are .class files.

    I see a few caveats though:

    • As with Java, you can only see classes visible to a certain classloader (this may be a problem, e.g. in OSGi applications)
    • The operation is expensive enough in Java, and even more so in Scala, as scalac generates a lot of additional classes for all the anonymous functions generated in your code, which can be a significant number owing to methods like filter, map, flatMap, etc. You may try to filter them out based on their name.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there some way I can use URLs like: http://www.blog.com/team-spirit/ instead of http://www.blog.com/?p=122 in
Is there any way I can detect when my page has been set as
Is there a way I can set up callbacks on (or automataically log) method
is there way thats i can preselect an item when the page loads or
Is there a way I can control columns from code. I had a drop
Is there a way I can access (for printout) a list of sub +
Is there a way you can run your UnitTest project against a compiled DLL
Is there some way I can define String[int] to avoid using String.CharAt(int) ?
Is there a way I can create Windows firewall exceptions in VB2005? I'm trying
Is there any way I can specify a standard or custom numeric format string

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.