Support for Java 8 syntax in aspects

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 (9)

  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. 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.

  3. 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.

  4. Log in to comment