2005/5/10

     
 

AAL Closures

artefaktur

| Source Code | Preprocessor | Comments | Literals | Classes | Closures | Functions | Function Types |

With Closures an inplace class can be defined.

Content of this chapter:

     Definition
     Samples

 Definition

new (ClosureParams) SuperDecls { Statements }

 Samples

int i;
new (i) { public int getI() { return i; } }
new (membername: 42) { public int getI() { return membername; }
Closure, which can be used as instance
  interface IntAdder { public int operator+(int j); }
  int i = 42;
  IntAdder adder = new (i) implements IntAdder 
                      { 
                        public int operator+(int j) { return i + j; } 
                      };
  int j = adder + 4;
  // j == 46
  
  // This Code will internally tranformed to:
  class Closure1 extends IntAdder
  {
    public Closure1(int i_) : i(i_) {}
    public int operator+(int j) { return i + j; } 
  }
  IntAdder adder = new Closure1(i);
  
Closure as simple code block
  interface Dumper { public void operator()(String s); }
  Dumper dumper = new () implements Dumper 
                      { 
                        public void operator()(String s) { System.out.println(s); }
                      };
  dumper("asdf");