A syntax option (function types versus arrays)
Neal Gafter
neal at gafter.com
Mon Mar 1 15:42:26 PST 2010
On Mon, Mar 1, 2010 at 2:13 PM, Alex Buckley <Alex.Buckley at sun.com> wrote:
> Thanks for recording a specific infix notation here, and the reasoning
> behind it. I am not opposed to infix notation, but I am opposed to
> saying that the type system will never allow arrays of function types.
> You and I did a JavaOne presentation in 2008 that majored on not
> restricting future evolution just because it helps with the syntax
> today.
That wasn't the reason for the restriction. The reason was that
reification would be a breaking change, and therefore will not happen.
So there's no need to plan for it. By comparison to the infix
notation the other forms look clumsy. It doesn't make sense to
cripple the function type syntax to support something that is and
always will be unsafe.
In any case, my proposed syntax doesn't prevent you from handling
arrays of function type, it just requires (like every problem in
computer science) a bit of indirection. For example, one could hold
arrays using this
public class ArrayHolder<T> {
public final final T[] array;
public ArrayHolder(T[] array) {
this.array = array;
}
}
Now, one can store the array from inside a varargs method:
void method(()->int ... numberGenerators) {
ArrayHolder<()->int> gens = new ArrayHolder<>(numberGenerators);
}
And from there pass them around all you want. This is unsafe, so the
syntactic gyrations don't bother me.
> So can you comment on further grammar changes to allow
> parentheses round the function type:
>
> ((A throws X)->Y)[] y; // Array of function-typed values
I agree with Rémi that you don't need so many parens. You could put
the throws either before or after the result type. I'd also suggest
keeping the lambda and function type syntax forms parallel in part
because the lambda is the primary, and currently only, way to get a
value of function type. For example either
(String -> int throws SomeException) variable = (String x -> 3);
or, as Rémi suggests
(String throws SomeException -> int) variable = (String x -> 3);
So the curry example would be something like
static <T,U,V,X extends Throwable>
(T->(U throws X->V)) curry((T,U throws X->V) function) {
return (T t->(U u->function.(t,u)));
}
Cheers,
Neal
More information about the lambda-dev
mailing list