Support for Java 8 syntax in aspects

Create issue
Issue #192 resolved
Ismail Badawi created an issue

It would be nice to be able to use method references (A::b) and lambdas (() -> { }) in aspects.

Comments (10)

  1. Jesper Öqvist

    This is not a priority to fix right now, but it would definitely be nice to support these features. I have been thinking about replacing the JastAdd parser and reuse the ExtendJ parser, which would fix this problem but would also cost too much time.

  2. Former user Account Deleted

    Is there a chance for this to be prioritized higher? I would love to be able to use the "new" syntax.

  3. Jesper Öqvist

    I think it's time to postpone the plan to replace the existing JastAdd parser and just add Java 8 features to the one we have now.

    I did some testing with lambda expressions and it seemed to work. I'll see if it is possible to add method references too.

  4. Jesper Öqvist

    I added parsing for lambda expressions, method references, and try-with-resources statements. I might have missed some cases, but at least these lambda variations work now:

    aspect Java8_Lambdas {
      // 1. Expression lambdas.
    
      // Single untyped lambda parameter.
      syn F1 A.untyped1() = i -> i * 2;
    
      // Multiple untyped lambda parameters.
      syn F2 A.untyped2() = (a, b) -> a / b;
      syn F3 A.untyped3() = (a, b, c) -> a + b - c;
    
      // Single typed lambda parameter.
      syn F1 A.typed1() = (int i) -> i * i;
    
      // Multiple untyped lambda parameters.
      syn F2 A.typed2() = (int a, int b) -> a * b;
      syn F3 A.typed3() = (int a, int b, int c) -> a * b / c;
    
      // 2. Block lambdas.
    
      // Single untyped lambda parameter.
      syn F1 A.untyped1block() = i -> { return i; };
    
      // Multiple untyped lambda parameters.
      syn F2 A.untyped2block() = (a, b) -> {
        if (a > b) {
          return a - b;
        } else {
          return b - a;
        }
      };
      syn F3 A.untyped3block() = (a, b, c) -> {
        if (a > b) {
          return (a - b) * c;
        } else {
          return (b - a) * c;
        }
      };
    
      // Single typed lambda parameter.
      syn F1 A.typed1block() = (int i) -> { return i * i; };
    
      // Multiple untyped lambda parameters.
      syn F2 A.typed2block() = (int a, int b) -> { return a * b; };
      syn F3 A.typed3block() = (int a, int b, int c) -> { return a * b / c; };
    
      // Function types:
      interface F0 { int apply(); }
      interface F1 { int apply(int a); }
      interface F2 { int apply(int a, int b); }
      interface F3 { int apply(int a, int b, int c); }
    }
    

    If you find Java 8 syntax variations that don't work, please comment here or make a new issue so we can add it to the parser.

  5. Jesper Öqvist

    FYI, you can download the latest JastAdd Jar (including this fix) from here: http://jasttest.cs.lth.se:8080/view/JastAdd/job/JastAdd2/lastSuccessfulBuild/artifact/jastadd2.jar

  6. Former user Account Deleted

    This is great news, thanks! The only case that seems to be missing is a lambda expression without parameters:

    Supplier<String> supplier = () -> "foo";
    
  7. Log in to comment