function types syntax
Rémi Forax
forax at univ-mlv.fr
Tue Jan 5 06:14:19 PST 2010
Le 05/01/2010 14:36, Peter Levart a écrit :
> Strawman proposal specifies the following syntax for function types (without throws declaration for now, but it suggests that it could be added later):
>
> # Type ( TypeList opt ) ThrowsDecl opt
>
> For example:
>
> #void() - a function taking no arguments returning nothing
> #BigDecimal(String) throws ParseException - a function taking a single String returning BigDecimal throwing ParseException
> ##int(int)(#int(int)) - function taking a single (function taking a single int returning int) returning a function taking a single int returning int
>
> I'm wondering if the leading '#' is necessary. Would it be ambiguous to leave it out?
I think there is a problem with the cast and parenthesis between
expression :
(A())
can be read as a cast to a function that returns a A or as a call to
function A
between parenthesis.
> The above examples would then read:
>
> void()
> BigDecimal(String) throws ParseException
> int(int)(int(int))
>
> I think someone mentioned that similarity between the syntax-es of function types and lambda expressions is actually not a good thing.
>
someone = me
But I prefer use # for function type and introduce a contextual keyword
named lambda for lambda expressions
lambda(int x) (x) instead of #(int x) (x)
lambda is a contextual keyword because it can be used anywhere as a
variable name or as a type
without a compile time error. So there is no source backward
compatibility problem.
The only problem with this approach is that you can't use () to call a
function type,
you have to use something like .invoke()
#void(int) f = ...
f.invoke(3); // instead of f(3);
And frankly try to write a lambda that takes a function type is not
humanly readable
with the syntax of the strawman proposal:
#(#int() f) (f())
This is a little better with the keyword lambda:
lambda(#int() f) (f.invoke())
It also better fit with current reflection API because it doesn't
require change.
> What do you think?
>
> Regards, Peter
>
>
regards,
Rémi
More information about the lambda-dev
mailing list