Reified Lambda Functions
Howard Lovatt
howard.lovatt at iee.org
Sat Jan 16 15:02:53 PST 2010
Hi Neal,
Sorry for taking so long to reply, I was on holiday with limited
connectivity.
Yes there is a bug in the specification, I have updated section 2.1, 4.3.1,
4.3.2, 5.2.2, and 5.3.2 and added sections 4.4 and 5.4 - thanks. (At the
same time, but unrelated, I have added section 1.1 and changed the
topographical convention used, swapping the roles of _ and $.)
With the updated specification the reified lambdas are little different than
any other example with generics. Contrast:
static <T> List<T>[] makeArrayOfList(final int size) { return
(List<T>[])new List[size]; }
static void makeArrayOfStringListTest() {
final List<String>[] arrayOfList = makeArrayOfList(1);
arrayOfList[0] = new ArrayList(1);
arrayOfList[0].add("Hello from a List");
System.out.println("ArrayOfStringList(0) = " + arrayOfList[0].get(0));
}
With:
static <T> #T()[] makeArrayOfLambda0(final int size) { return #T()[size];
}
static void makeArrayOfStringLambda0Test() {
final #String()[] arrayOfLambda0 =
Examples.<String>makeArrayOfLambda0(1); // I doubt the type inference will
cope, hence qualification
arrayOfLambda0[0] = #String() ("Hello from a Lambda0");
System.out.println("ArrayOfStringLambda0[0].call() = " +
arrayOfLambda0[0].call());
}
Which is translated to:
static <T> _Callable_0<? extends T>[] makeArrayOfLambda0(final int size) {
return (_Callable_0<? extends T>[])new _Callable_0[size]; }
static void makeArrayOfStringLambda0Test() {
final _Callable_String[] arrayOfLambda0 =
_Array_From_0_String__To_String.instance(Examples.<String>makeArrayOfLambda0(1));
arrayOfLambda0[0] = new _Callable_String() {
@Override public String call() { return "Hello from a Lambda0"; }
};
System.out.println("ArrayOfStringLambda0[0].call() = " +
arrayOfLambda0[0].call());
}
Thanks again for pointing out the problem,
-- Howard.
2010/1/8 Neal Gafter <neal at gafter.com>
> On Fri, Jan 8, 2010 at 2:19 PM, Howard Lovatt <howard.lovatt at iee.org>
> wrote:
> > I am not sure what you have in mind, do you have an example I could try
> > coding up.
>
> <T> #T()[] makeArray(int size) {
> return new #T()[size];
> }
>
> and a client
>
> #String()[] array = makeArray(20);
>
> ______________________________________________________________________
> This email has been scanned by the MessageLabs Email Security System.
> For more information please visit http://www.messagelabs.com/email
> ______________________________________________________________________
>
--
-- Howard.
More information about the lambda-dev
mailing list