Lets say that I have a need to hold an arbitrary function f
within a Node
case class Node[A](f: Any -> Any)
(I couldn't find any type around Function
or Callable
. Scala core just defines FunctionN
s)
This node will then randomly sample arguments from a Terminal Set
which it is going to use to call f
.
So it needs to know two things:
- the arity of
f
- how to invoke
f
Additional:
I can retrieve the arity of a method object using
def arity(f: AnyRef): Option[Int] = {
val apply = f.getClass.getMethods.find(_.getName == "apply")
apply.map(_.getParameterCount)
}
def add(x: Int, y: Int) = {
x + y
}
arity(add _)
However calling arity(f _)
(within a Node
instance) no longer works.
Besides this, I still have the problem of how to invoke f
If you have suggestions for an alternative design approach please let me know.
Aucun commentaire:
Enregistrer un commentaire