- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Making reference to methods or object attributes.
Wed, 2012-01-25, 11:40
Dear all,
I am looking for a mean to express something that looks like a
closure: a reference to a method or an attribute.
I know that I can express it with a closure like that:
val myRef : MyObject => MyValue = _.myMethod
My problem is that I need a mean to compare such references, which is
AFAIK not possible with standard closure.
I would like something such as:
val myRef1 : String => Int = _.length
val myRef2 : String => Int = _.length
assert(myRef1 == myRef2) // actually throw assertion error
"Thanks to Turing", comparing closure is not possible in the general
case. What I need is a way to compare trivial closures. My criteria
for triviality is when I can call them method reference (or attribute
reference).
Any clue?
Thank you
Emmanuel
Thu, 2012-01-26, 12:01
#2
Re: Making reference to methods or object attributes.
Thank you for your reply.
Now, I know that there is no obvious way to do it.
I am considering the compiler plugin way.
On 25 jan, 18:06, Paul Phillips wrote:
> On Wed, Jan 25, 2012 at 2:40 AM, Emmanuel Castro wrote:
> > "Thanks to Turing", comparing closure is not possible in the general
> > case. What I need is a way to compare trivial closures. My criteria
> > for triviality is when I can call them method reference (or attribute
> > reference).
>
> > Any clue?
>
> You could write a compiler plugin to record the name of method which was
> eta-expanded into the closure as an annotation on the classfile. (Or you
> could do any number of things like that if you will be in control of where
> these closures come from.) Otherwise you probably have to use a bytecode
> library and look at the instructions. A "trivial closure" should have a
> (possibly specialized) apply method, where the penultimate instruction is
> an invokevirtual call to some method. The target of that invokevirtual is
> your basis for comparison.
On Wed, Jan 25, 2012 at 2:40 AM, Emmanuel Castro <manu.ecl@gmail.com> wrote:
You could write a compiler plugin to record the name of method which was eta-expanded into the closure as an annotation on the classfile. (Or you could do any number of things like that if you will be in control of where these closures come from.) Otherwise you probably have to use a bytecode library and look at the instructions. A "trivial closure" should have a (possibly specialized) apply method, where the penultimate instruction is an invokevirtual call to some method. The target of that invokevirtual is your basis for comparison.