English 中文(简体)
Scala 3 反射: 收集所有属于单子物体的成员
原标题:Scala 3 reflection: collect all members that are singleton objects
I have a method in Scala 2 that uses reflection. It doesn t work in Scala 3, and I would like to reimplement it so it works for Scala 3. (I will no longer need it for Scala 2 anymore, so it need not work for both). The purpose of the method is to examine an object and collect any instances of singleton objects it has as members, in order of declaration. (It is known by the programmer ahead of time that all such singleton objects extend A). It is working correctly. def allSingletonObjects[A](host: Any): Vector[A] = { import scala.reflect.runtime.universe.runtimeMirror val mirror = runtimeMirror(host.getClass.getClassLoader) mirror .classSymbol(host.getClass) .info .members .filter(_.isModule) .map(sym => mirror.reflectModule(sym.asModule).instance.asInstanceOf[A]) .toVector .reverse } Here s an an example of how it is used. sealed trait Color object Color { case object Red extends Color case object Green extends Color case object Blue extends Color } In this example, allSingletonObjects[Color](Color) would return Vector(Color.Red, Color.Green, Color.Blue). How would I implement allSingletonObjects in Scala 3? Note: I am not able to change the way I am modeling the types (e.g. using enums). I would ideally just like allSingletonObjects to work as it did in Scala 2.
问题回答
I agree with the comments, you should probably find a better way to do this, but here is a working version: import scala.quoted.* inline def allSingletonObjects[A](host: Any) = allSingletonObjectsSeq[A](host).toVector inline def allSingletonObjectsSeq[A](host: Any) = ${allSingletonObjectsSeqImpl[A]( host)} def allSingletonObjectsSeqImpl[A: Type](host: Expr[Any])(using quotes: Quotes): Expr[Seq[A]] = import quotes.reflect.* Expr.ofSeq: host .asTerm .tpe .classSymbol .get .declarations .filter(sym => sym.flags.is(Flags.Case | Flags.Final | Flags.Lazy | Flags.Module | Flags.StableRealizable)) .map(sym => Ref(sym).asExprOf[A])




相关问题
c# reflection with dynamic class

I need to execute a method "FindAll" in my page. This method returns a list of the object. This is my method that I execute "FindAll". FindAll requires an int and returns an List of these class. ...

Performance overhead of using attributes in .NET

1.. Is there any performance overhead caused by the usage of attributes? Think for a class like: public class MyClass { int Count {get;set;} } where it has 10 attibutes (...

WPF GridView, Display GetType().Name in DisplayMemberBinding

I want include the Type name of each object in my collection from my GridView. I have a collection which has four different types in it that all derive from a base class. Call them, Foo, Bar, Fizz, ...

Testing private method of an abstract class using Reflection

How can I test a private method of an abstract class using reflection (using C#)? I am specifically interested in adapting the code found in this thread. I am aware of the discussion around the ...

Adding Items to ListBox, RadioList, Combobox using reflection

I m trying to add items to a listbox,combobox, radiolist using reflection. The code I have at the moment is as follows: public static Control ConfigureControl(Control control, ControlConfig ctrlconf)...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

热门标签