Closure Impl. in Java

2008/04/01 14:42

두 정수의 합 구하기 Closure Literal.

  1.  
  2. {int x, int y => x+y}  

두 정수의 합 구하기 Closure를 지역 변수 plus에 할당하기

  1.  
  2. {int,int=>int} plus = {int x, int y => x+y};  

내부적으로는 위와 같은 literal들이 translation 됩니다. 예를들어,

  1.  
  2. {int,String=>Number throws IOException} xyzzy;  

와 같은 변수 선언은

  1.  
  2. interface Closure1<R,A2,throws E> { // system-generated  
  3.     R invoke(int x1, A2 x2) throws E;  
  4. }  
  5. Closure1<? extends Number,? super String,null> xyzzy;  

와 같이 변환됩니다.

Control Invocation Syntax의 예

  1.  
  2. withLock(lock) {  
  3.     System.out.println("hello");  
  4. }  

file 처리의 예

  1.  
  2. with(FileReader in : makeReader()) with(FileWriter out : makeWriter()) {  
  3.     // code using in and out  
  4. }  

    흐미..