Straw man section 7: Method References

David Goodenough david.goodenough at linkchoose.co.uk
Mon Dec 14 07:16:31 PST 2009


Paul,

As understand it, # only helps grab a method literal if the thing to
the right of the # is a method, if it were a field, then if would grab 
a field literal.

I am not really sure having read the two links provided in section 7 that I 
see how method literals are encoded in BGCA, but in FCM it would be:-

rhs[#lhs]

where rhs is either an object or a class, and you have a chain of 
methods or fields on the right.  This allows you to create a reference
to any existing method or field.  Allowing a chain of methods at first 
sight might seem odd, but it is certainly needed on the case of field
references.  

Actually allowing it in the case of a method reference might obviate the need
to have field references at all, so if you had a an object foo of type Foo
with a field bar of type Bar which in turn had a field jim of type String then
to get at the field in a manipulatable way one could either encode:-

FieldRef<Foo,String> ref = foo#bar#jim;
or
MethodRef<Foo,String> ref = foo#getBar()#getJim();

This latter has the merit of not need the bending of the visibility rules,
so if bar or jim are private fields, but their getters are public the former 
needs rules rather different to the normal "." rules, while the second
form does not.  The first version requires an assumption that while
the field might be private, the getter is public even though it is not 
actually the getter that is referenced.

The second form looks less intuative to use, but may be better.

It is necessary to use a FieldRef or MethodRef (for want of better names)
rather than Field and Method directly so that compound access (multiple
#s) can be used.  This is necessary in many of the field referencing
cases I could produce, but not in the case of simple method references 
that were envisenged in the original Method Reference proposals.   A 
Field/MethodRef also simplifies the question as to whether it is a bound 
reference or an Unbound one, in the second example above the
unbound equivalient is Foo#getBar()#getJim() where this can be applied
to any object of class Foo and you do not need an instance at the time
that you create the reference.

David

On Monday 14 December 2009, Paul Benedict wrote:
> David,
> 
> I find your suggestion interesting. If # helps grab a method literal,
> what syntax can grab a field literal? Do you have anything in mind?
> 
> Paul
> 
> On Mon, Dec 14, 2009 at 6:30 AM, David Goodenough
> 
> <david.goodenough at linkchoose.co.uk> wrote:
> > I realise that this section is TBD, but can I ask whether Field
> > References could also be considered at the same time.  I realise that
> > they are not really part of closures, but once the basic syntax is in
> > place for Method References then Field References are a simple 
extension.
> >
> > I can outline why I think it important to do Field References, but if you
> > regard it as totally out of court there is little point in starting.  I
> > would however be very keen to help if this could be added, and I believe
> > that it would benefit Java to add this functionality. It is not a large
> > change, there are no JVM changes required and only one extra library
> > class required of which I already have a prototype.
> >
> > David
> 



More information about the lambda-dev mailing list