f(x) syntax sugar in Lambda
Maurizio Cimadamore
maurizio.cimadamore at oracle.com
Thu Feb 9 01:46:09 PST 2012
Hi,
I think there might be few problems with this approach when unleashed in
a more widespread way:
*) Java has two separate namespaces for methods/fields - so the
following would be ambiguous:
Exponent exp = (x) -> 1 + x + x * x / 2;
void exp(int x) { ... }
exp(3) //?
*) The language does not have such a thing as 'type of a lambda' so,
there is no way to distinguish between:
SAM s = ()-> { ... }
and
SAM s = new SAM() { ... }
This means that your proposal will apply to all expressions whose type E
is a functional interface.
*) What if E is a method call? You end up with things like:
Exponent makeExp() { ... }
makeExp()(5);
*) If you have a functional interface with default methods:
interface Exponent {
double calculate(double x);
void foo() default { ... }
void bar() default { ... }
}
then the fact that the syntactic sugar will rewire the method call to
'calculate' (as that's the only abstract method in Exponent) might
result a bit surprising since there is more than one method available in
the functional interface.
Maurizio
On 09/02/12 07:41, Spot Al wrote:
> Hi,
>
> For example if there is an interface
> -----------------------------------------------
> interface Exponent{
> double calculate(double x);
> }
> -----------------------------------------------
>
> and I have a lambda expression:
> -----------------------------------------------
> Exponent exp = (x) -> 1 + x + x * x / 2;
> -----------------------------------------------
>
> would it be possible to get a value like:
> double e = ep(1);
> instead of
> double e = exp.calculate(1); ?
>
> Where exp(1) just a syntax sugar for the exp.calculate(1).
>
> So if variable a has a lambda type (interface with one method) than a(val1,.., valN) means a.methodName(val1,.., valN)?
>
> Thanks,
> Alexander.
>
>
>
>
>
More information about the lambda-dev
mailing list