One more syntax option allowing nice control abstraction
Alexander Kochurov
alexander.kochurov at maxifier.com
Mon Jun 20 21:42:29 PDT 2011
'lambda' may be a context-dependent keyword. Compiler can easily distinguish
weather 'lambda' is used as keyword or as field/class/method name: right
bracket may be followed by '{' only in method definition, anonymous class
definition, try-catch statement or lambda definition. It complicates
grammar, but is still possible.
On Tue, Jun 21, 2011 at 1:52 AM, David Goodenough <
david.goodenough at linkchoose.co.uk> wrote:
> On Monday 20 Jun 2011, Reinier Zwitserloot wrote:
> > Much bigger drawback you failed to mention: Breaks all java code that
> uses
> > 'lambda' as an identifier name.
> True, but this is always going to be true with new keywords. The
> alternative
> is to add yet more meanings to existing keywords (do) or finding a symbol
> that
> is not yet used - both have their disadvantages.
>
> David
> >
> > --Reinier Zwitserloot
> >
> >
> >
> > On Mon, Jun 20, 2011 at 9:21 PM, Alexander Kochurov <
> >
> > alexander.kochurov at maxifier.com> wrote:
> > > David Goodenough suggested to use 'lambda' keyword:
> > > "lambda()(5)
> > > lambda(){return 5;}
> > >
> > > lambda(int x,int y) { if (x>y) return x; else return y; }"
> > >
> > > Control abstraction may be introduced later in the following way:
> > >
> > > to pass lambda to any method taking SAM type as it's last argument,
> > > lambda body should be written in braces after closing bracket, lambda
> > > argument names and types may be given before method parameters in
> > > brackets, separated
> > > by colon (if any):
> > >
> > > sort(T[], Comparator<T>) may be called with lambda comparator as
> > >
> > > T[] array = ...
> > > sort(T a, T b: array) { // a & b are lambda arguments; array is
> > > parameter of 'sort' method
> > >
> > > return a.compareTo(b);
> > >
> > > }
> > > 'lambda' may also be defined as a plain java function (but this won't
> > > work cause type for SAM convertion should be known at compile time):
> > > <T> T lambda(T t) { return t; }
> > > * Pros:
> > > IMHO, that syntax looks very java-ish: see for loop for maps definition
> > > below
> > > <K, V> void forEachEntry(Map<K, V> map, Function2<K, V>*/ f) {
> > >
> > > for (Iterator<Entry<K, V>> it =
> > >
> > > map.entrySet().iterator();it.hasNext();) {
> > >
> > > Entry<K, V> entry = it.next();
> > > f.apply(entry.key, entry.value);
> > >
> > > }
> > >
> > > }
> > > Map<K, V> m = ...
> > > forEachEntry(K k, V v: m) {
> > >
> > > System.out(k + " => " + v);
> > >
> > > }
> > > *drawback*: constructor cannot use that syntax: it would be
> > > indistinguishable
> > > from subclassing:
> > > new Type() { /* is that labda body or anonymous class body? */ }
> > > Lambdas should be constructed explicitely before passing it to
> > > constructor: new Type(lambda() {return someValue;}) { /* this is not
> > > lambda body, it's anonymous type body*/ };
> > >
> > > Alexander Kochurov
>
>
>
More information about the lambda-dev
mailing list