3

Is there a way in scala to get list of all objects(by object I mean scalas object) that derive from specified abstract class? something like that:

abstract class A 
object B extends A //in different file
object C extends A //in different file

def findObjectsDerivingFromA(): Seq[A] //which will give result: Seq(B, C)

I know from here : Can I get a compile-time list of all of the case objects which derive from a sealed parent in Scala? that it's possible with sealed base trait/class but in my case objects B and C will be pretty complex so I need to have them in different files.

edit: I've changed method name because previous one was misleading.

Community
  • 1
  • 1
user2963977
  • 572
  • 5
  • 17
  • 1
    Are you talking about finding them within some collection or finding any instance that has been instantiated anywhere? – Randall Schulz Apr 11 '14 at 14:12
  • Of course there is no way to find those which are defined but not used (short of rummaging around through jars on some path), because they aren't even loaded. So for starters, would a Set/Seq containing the ones that are actually loaded suffice? – AmigoNico Apr 11 '14 at 23:11
  • I want to get scalas objects that are deriving from class A. I think those are created when application starts, am I correct? So probably best answer to @RandallSchulz question is "finding any instance that has been instantiated anywhere". – user2963977 Apr 12 '14 at 11:09
  • 1
    `Object` classes are loaded on-demand and the object itself initialized / instantiated when the first reference to them or any of their members is evaluated. Since they share a common superclass, you can record the `this` reference in some global `Set` or `Map` (which will have to live in a global object, of course). – Randall Schulz Apr 12 '14 at 16:25
  • You are right - objects are lazy. I've ended up with adding those objects by hand. Thx for hints/explanations. – user2963977 Apr 15 '14 at 05:54

0 Answers0