hg: lambda/lambda/langtools: Next round of implementation reflecting the latest 'State of the Lambda' draft; implemented features are:

Peter Levart peter.levart at marand.si
Tue Jul 27 06:44:03 PDT 2010


On 07/27/10, Maurizio Cimadamore wrote:
> On 27/07/10 10:37, Peter Levart wrote:
> > Hello Maurizio,
> >
> > The latest 'State of the Lambda' draft mentions optional "appearance" of target SAM type just before lambda body in cases where necessary to disambiguate overloaded method resolution or where context doesn't provide (correct) target SAM type. I haven't managed to find out where to stick this optional SAM type in current prototype. Simple cast doesn't work as Alex explained. Is this implemented already?
> >    
> 
> This works for me:
> 
> interface SAM<X> {
>    X m();
> }
> 
> class Test {
> <X> void call(SAM<X> s) { }
> 
> { call(SAM<Integer> #{ 1 }); }
> }
> 

This particular example works for me too, but try compiling this:

package closures;

public class Test {

  public interface SAM1<X> { X m(); }
  public interface SAM2<X> { X m(); }

  public static <X> void call(SAM1<X> s) { }
  public static <X> void call(SAM2<X> s) { }

  public static void main(String[] args) {
    call(SAM1<Integer> #{ 1 });
  }
}


I get:


Test.java:12: reference to call is ambiguous, both method <X#1>call(SAM1<X#1>) in Test and method <X#2>call(SAM2<X#2>) in Test match
    call(SAM1<Integer> #{ 1 });
    ^
  where X#1,X#2 are type-variables:
    X#1 extends Object declared in method <X#1>call(SAM1<X#1>)
    X#2 extends Object declared in method <X#2>call(SAM2<X#2>)
1 error


or this (which I tried in the first place):


package closures;

public class Closures 
{
  public interface ComparableF { Comparable invoke(); }
  public interface StringF { String invoke(); }

  public static void say(StringF stringFunc) {
    System.out.println("stringFunc says: " + stringFunc.invoke());
  }

  public static void say(ComparableF comparableFunc) {
    System.out.println("comparableFunc says: " + comparableFunc.invoke());
  }

  public static void main(String[] args) 
  {
    say(#{ 123 });
    say(StringF #{ "Hello" });
  }
}


I get:


Closures.java:19: ')' expected
    say(StringF #{ "Hello" });
               ^
Closures.java:19: not a statement
    say(StringF #{ "Hello" });
                   ^
Closures.java:19: ';' expected
    say(StringF #{ "Hello" });
                          ^
Closures.java:19: illegal start of expression
    say(StringF #{ "Hello" });
                            ^
4 errors


Regards, Peter

> 
> Maurizio
> > Regards, Peter
> >
> > On 07/23/10, maurizio.cimadamore at oracle.com wrote:
> >    
> >> Next round of implementation reflecting the latest 'State of the Lambda' draft; implemented features are:
> >>
> >> *) Lambda expressions
> >> New syntax. The non-terminal symbol '#' is still used to introduce both lambda expressions and function types, but a bunch of improvements have been made.
> >> Now, the lambda body is always denoted by braces '{' '}', as in #(int x) {}. Moreover, if the argument list is empty, it can be omitted, as in #{...}.
> >>
> >> *) SAM conversion&  Target typing
> >> Updated to latest specification. Lambda expression are now only allowed where a target type is expected (return/assignment/method call - and, for compatibility with old prototype). The target type can be either a function type or a SAM type. The target type is used for inferring partially specified lambda types, as in #(x) { ... }.
> >>
> >>      
> 
> 


More information about the lambda-dev mailing list