Given something like:
class A {
def f(x: X) = ...
def g(y: Y, z: Z) = ...
...
}
How to (automatically) extract the function(s):
object A {
val f' = (a: A, x: X) => a.f(x) // do this automagically for any arbitrary f
val g' = (a: A, y: Y, z: Z) => a.g(y, z) // and deal with arity > 1 too
...
}
With this exact type signature (first the object, then the parameter list). Let me state the problem very clearly:
"Given a method
f(x1: X1, ..., xn: Xn)
defined in the context of a classA
, how to automatically extract a functionf'
that (i) receives an instancea
of typeA
, and (ii) a parameter list that corresponds 1:1 to the parameter list off
, viz.x1: X, ... xn: Xn
, which implementation is exactlya.f(x1: X1, ..., xn: Xn)
"
Or even:
Capturing the concept of extensionality of lambda-calculus, such that you automatically extract an
λx.(f x)
fromf
wheneverx
does not appear free inf
.
This could first be tackled by finding a way access the identifiers f
, g
, ... without having a specific a: A
(one would have a specific A
later, of course). We could simply write f'
or g'
by hand, but let's indulge DRYness.
P.S. Maybe this isn't possible without runtime reflection (albeit it may be possible with Scala 2.10+ macros), since I can't seem to find a way to refer to the identifiers of f
or g
without a specific instance (a: A
) beforehand. But it would be something like the following, without having to resort to strings
:
A.getMethod("f"): Function2[A, X, ...]
I also realize that practical uses of the question may help the participants to suggest alternatives, but I'm discussing this in an abstract sense. I'm not trying to solve other problem which I've reduced to this one. I'm trying to know if this one is possible :-) Here's a very nice article to actually understand the motivation behind this question, with rants over Eta-expansions on Scala.