lundi 14 août 2017

How to retrieve the package and class info for an argument function in Scala?

Is there a way to get the full package and class info/names for a function passed as an argument? Imagine the following case:

package mypackage

class MyClass {
  def func(a: Int): Int = {
    a * a
  }

  def getPackageInfo(f: Int => Int): String = {
    // return package and class name of f
  }
}

In that case invoking getPackageInfo(func) somewhere in MyClass should return something like mypackage.MyClass.func.

I am aware that f might be an anonymous function that does not belong to a class, which could be handled by an extra check.

I also don't mind changing the signature of getPackageInfo, as long as it can accept methods and return their package and class info.





Aucun commentaire:

Enregistrer un commentaire