This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Making reference to methods or object attributes.

2 replies
Emmanuel Castro
Joined: 2012-01-25,
User offline. Last seen 42 years 45 weeks ago.

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

extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: Making reference to methods or object attributes.


On Wed, Jan 25, 2012 at 2:40 AM, Emmanuel Castro <manu [dot] ecl [at] gmail [dot] com> 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.
Emmanuel Castro
Joined: 2012-01-25,
User offline. Last seen 42 years 45 weeks ago.
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.

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland