Closure

From CometPublic

Jump to: navigation, search

Closures are one of COMET's advanced features and can be used as part of the control abstractions. A closure is a piece of code coupled with it's environment. The following example is a correction to the example found on Page 153 - 154 of the Constraint-Based Local Search book.

  class DemoClosure{
      DemoClosure() {}
      Closure print(int i) {
          return closure {
               cout << i << endl;
          };
      }
  }

Note that you could also write the closure declaration as follows.

  Closure print(int i) {
     closure c {
           cout << i << endl;
      }
     return c;
  }

Closures aren't executed until the call() method is executed. Since closures are first order expressions you can store closures in Arrays and pass them as arguments.

   DemoClosure cl();
   Closure c = cl.print(4);
   call(c);
Personal tools