Is there a way that I can “skip” a set of classes in a specified package in Scala?
A use case is to manage a set of services inherited from the BaseService attribute that receive an accessible name for the REST API. The Manager class should be able to provide a list of services, and also check whether the provided service exists, and, if so, create an instance to perform the overloaded function.
My thought is something like this pseudocode:
for ( clazz <- com.demo.pkg ) { val service = Class.forName(clazz).newInstance registerService( service ) }
Instead of instantiating, static methods on the object of the same name to provide the name and description of the service might be better.
In Python, this is trivial because of dir () and in PHP it is pretty simple because of the functions of the class loader, but I'm new to Scala.
In addition, I understand that I can approach this incorrectly and welcome feedback.
Update:
I accepted the JPP answer below, but it will fix it too expensive - a process for a normal operation. Therefore, I need to change my approach. Instead, the manager class will contain a static list of service clusters. Although this is not ideal from a developmental point of view, gaining speed while working seems to be worth it.
source share