Constructor references (was: Re: Problem with method references)

Peter Levart peter.levart at marand.si
Fri Jan 14 04:10:54 PST 2011


On 01/14/11, Maurizio Cimadamore wrote:
> On 14/01/11 09:41, Rémi Forax wrote:
> > [...]
> >
> >>> Hello Maurizio,
> >>>
> >>> I see that referencing instance methods from static context is a desirable feature, but syntax-wise I aggree with Neal that the same expression should not change its meaning if moved from static to instance context or vice-versa (when both contexts are legal). So I propose that the syntax:
> >>>
> >>>      #methodName
> >>>
> >>> be made illegal for referencing instance methods from static context.
> >> I agree - since #methodName is essentially a shortcut for
> >> this#methodName, it is sensible to reject it in a static context.
> >> Thanks for the example - I will make sure to add it to our regression
> >> tests as soon as I add support for #this.
> > It remember something, do we have a plan to support constructor call, I
> > mean not
> > constructor call from a constructor (this(...)) but constructor call
> > that return a newly
> > created object.
> >
> > Something like:  new#Integer(int)
> > which should be typed (int) ->  Integer.
> That sounds entirely reasonable. I see few problems in the following cases:
> 
> *) inner class creation, which might depend on an enclosing instance - 
> do we want to treat the enclosing instance as an additional parameter?
> 
> *) qualified new expressions - in this case it seems sensible to just 
> add the explicit enclosing class reference to the MH (i.e. through bind).
> 
> Maurizio
> >> Maurizio
> > Rémi
> >

If we want to be syntactically consistent with constructor invocations then, analogous to method references with infix '#',  constructor references syntax could be modeled as following:

public class Top
{
    class Inner
    {
        Inner(String s)
        {
        }
    }

    static class Nested
    {
        Nested(String s)
        {
        }
    }

    public Top()
    {
        // constructor invocations

        Top.Inner inner1 = this.new Inner("yyy");
        Top.Inner inner2 = new Inner("yyy"); // equivalent as above

        Top.Nested nested1 = new Top.Nested("bbb");
        Top.Nested nested2 = new Nested("bbb"); // equivalent as above

        // constructor references

        StringToInner f1 = this#new Inner;
        StringToInner f2 = #new Inner; // equivalent as above
        TopStringToInner f3 = Top#new Inner;

        StringToNested f4 = #new Top.Nested;
        StringToNested f5 = #new Nested; // equivalent as above
    }

    static
    {
        Top top = new Top();

        // constructor invocations

        Top.Inner inner = top.new Inner("xxx");
                       // new Inner("xxx"); - illegal

        Top.Nested nested1 = new Top.Nested("aaa");
        Top.Nested nested2 = new Nested("aaa"); // equivalent as above

        // constructor references

        StringToInner f1 = top#new Inner;
                        // #new Inner; - illegal
        TopStringToInner f2 = Top#new Inner;

        StringToNested f3 = #new Top.Nested;
        StringToNested f4 = #new Nested; // equivalent as above
    }

    interface StringToInner
    {
        Inner apply(String s);
    }

    interface TopStringToInner
    {
        Inner apply(Top t, String s);
    }

    interface StringToNested
    {
        Nested apply(String s);
    }
}


Regards, Peter


More information about the lambda-dev mailing list