Array constructor references for multidimensional array
daniel.smith at oracle.com
Wed May 8 07:10:37 PDT 2013
On May 7, 2013, at 10:09 PM, bitter_fox <bitterfoxc at gmail.com> wrote:
> Array constructor references had been supported by .
> I have a question about multidimensional array constructor references.
> In my example, I use a two dimensional array of int, but other
> multidimensional arrays have a same problem.
> We can write references for the one argument constructor of int("new
> int[x]") by the change of :
> interface F1
> int f(int a);
> F1 f1 = int::new;
> f1.f(5); // means "new int"
> We have another form of the array creation("new int[x][y]"), however the
> compiler rejects references for it.
> interface F2
> int f(int a, int b);
> F2 f2 = int::new; // error
> f2.f(5, 7);
> Is this the expected behavior?
> I think it would be accepted because we can consider them as a two argument
> constructor and it should treat them like a constructor of other type which
> is not array.
> Is this already discussed?
There's a trade-off. This makes the expressions a little more powerful, but adds some complexity. I'm not uncomfortable with the current behavior as a stable point, but I don't know how much others have thought about it and can raise the question with the Expert Group.
Method references are essentially shorthand for lambda expressions. We've selected a few common forms of lambda expressions to give them special treatment. But there will always be something just slightly different that lives outside of the set we've chosen to support. And in that case, the solution is to use a lambda expression instead:
F2 f2 = (x,y) -> new int[x][y]; // vs. int::new
Another form that isn't supported by method references, but that someone might wish for, is one in which some method parameters are bound early:
Function<Integer,String> f = i -> s.substring(0,i); // vs. s::substring(0,_) (for example)
More information about the lambda-dev