void handling in expression lambdas and lambda conversion

Peter Levart peter.levart at gmail.com
Wed Jan 13 13:45:57 PST 2010


On Wed, Jan 13, 2010 at 7:21 PM, Neal Gafter <neal at gafter.com> wrote:

> >From JLS3 15.1: "An expression denotes nothing if and only if it is a
> method invocation (§15.12) that invokes a method that does not return
> a value, that is, a method declared void (§8.4)."
>
> Clearly, an expression may denote nothing.  It goes on to say: "Such
> an expression can be used only as an expression statement (§14.8),
> because every other context in which an expression can appear requires
> the expression to denote something."
>

Yes, my suggestion was to add expression lambda as the valid use case of a
"void" expression.

>

>
> CfJ 0.6a currently does not have such a requirement, so either the
> requirement would be added (the expression of a lambda expression must
> denote a value) or this sentence in 15.1 should be relaxed.  I don't
> have strong feelings one way or another.
>


> > My first question is: Would these be considered valid assigments:
> >
> > #void() lambda1 = #() (Void)null;
>
> The lambda conversion as written doesn't allow this, but I think
> that's a bug in the spec.  0.6b depends on it working.  I've updated
> the 0.6a spec and added your name to the acknowledgments.
>
> > #void() lambda2 = #() 1;
>
> Not currently.  Can you explain why it would be a good idea to allow this?
>

I was just wondering if there was some hidden rule (I was not aware of) that
allows Void to be assigned to void (1st case) then it might as well allow
the 2nd case. I don't think such a rule would be a good idea.


> > The other question is: Would it be possible to relax constraints for the
> expression specified in expression lambda to allow a method invocation of a
> void method?
>
> Sure, it would be possible.  What is the benefit?
>
>
I think it is a good fit. Invocation of a "void" method is a "void"
expression. Invocation of a #void() function is also a "void" expression. So
if this is a valid expression statement:

voidMethod();

Why should this be not:

(#() voidMethod()) ();

(Tennent's principle for expressions?)

> I know that "Method References" are even a more compact syntax, but as
> specified, are not powerfull enough for specifying this:
> >
> > int a = 1;
> > #void(int) lambda = #(int b) method(a, b);
>
> True, but you could write this, which is arguably more clear:
>
> #void(int) lambda = #(int b) { method(a, b); }
>

It is clear, but the intention is different. The intention of:

#void(int) lambda = #(int b) { method(a, b); }

is to construct a nothing-returning lambda regardless of what the method
returns. The intention of:

#void(int) lambda = #(int b) method(a, b);

is to construct a lambda that has the same or at least assignment
compatible return type as the method.


>
> Cheers,
> Neal
>

Regards, Peter
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.openjdk.java.net/pipermail/closures-dev/attachments/20100113/84908c74/attachment.html 


More information about the closures-dev mailing list