Lightweight interfaces instead of function types
Alex Buckley
Alex.Buckley at Sun.COM
Sat Feb 20 22:46:56 PST 2010
Stephen Colebourne wrote:
> On 19 February 2010 17:47, Neal Gafter <neal at gafter.com> wrote:
>> Tmp.java:5: [error] generic array creation
>> Callable<String>[] array = new Callable<String>[10];
>> This is a hard error, not a warning.
>
> True. But if I want a Callable<String>[] I write:
>
> Callable<String>[] array = new Callable[10];
>
> This works, and is safe /in practice/ (rather than type-system safe).
No, it isn't safe in practice. It causes heap pollution which can cause
programs to crash. It is a hack allowed because migration compatibility
was so very, very important. (It is not particularly helpful of IDE
vendors to hide the unchecked warning by default.) Reified generics
don't help either; they just make the topic 5x more complicated.
> Again, its a trade-off. The benefit for ParallelArray is that
> Predicate or Transformer is IMO more meaningful to someone just
> casually coming to the API than a random function type.
I actually agree with this. Long nominal type names are the Java way.
Unfortunately, we don't have reified generics over primitive type
arguments (Predicate<int>, Ops.Reducer<int,int>). This is because we
don't know how to implement them efficiently, even if strides have been
made over the past decade w.r.t. implementing reified generics over
reference type arguments.
Reifying function types as MethodHandles still has no specialization for
primitive types, so won't give Doug what he needs. Long story short:
first-class functions in the Java language may be doomed without fully
reified structural types in the Java VM. (Other languages can avoid
primitive types or box them to their hearts' content, but again, that
doesn't help Java programmer or Doug's library.)
Hence my "peering over the edge of a very tall cliff" comment last week.
Alex
More information about the lambda-dev
mailing list