구조적 매칭

구조적 매칭 (Structural Matching)

기존 단위(unit)의 구현을 그대로 쓰면서 import·export·초기화 의존성만 다시 선언할 수 있게 해 주는 폼들을 살펴볼게요. 단위의 연결 지점을 재구성할 때 유용해요.

출처: Racket Reference

본문

syntax

(unit/new-import-export
  (import tagged-sig-spec ...)
  (export tagged-sig-spec ...)
  init-depends-decl
  ((tagged-sig-spec ...) unit-expr tagged-sig-spec))

unit과 비슷하지만, 단위의 본문은 unit-expr이 만들어 내는 기존 단위에 의해 결정돼요. 결과는 구현이 unit-expr인 단위인데, 그 import·export·초기화 의존성은 (단위 unit-expr이 만든 것과 달리) unit/new-import-export 폼에 쓰인 그대로예요.

unit/new-import-export 폼의 마지막 절이 이전/새 import·export 사이의 연결을 결정해요. 이 연결은 compound-unit이 import·export를 전파하는 방식과 비슷한데, 차이점은 연결 절의 import와 오른쪽 사이의 연결이 시그니처의 이름이 아니라 시그니처 안 요소의 이름에 기반한다는 거예요. 즉 연결 절 오른쪽의 tagged-sig-spec이 import 절에 tagged-sig-spec으로 나타날 필요는 없지만, 연결하는 tagged-sig-spec이 함의하는 각 바인딩은 import 절의 어떤 tagged-sig-spec이 함의해야 해요. 마찬가지로 export tagged-sig-spec이 함의하는 각 바인딩은 연결 절의 어떤 왼쪽 tagged-sig-spec이 함의해야 해요.

syntax

(define-unit/new-import-export unit-id
  (import tagged-sig-spec ...)
  (export tagged-sig-spec ...)
  init-depends-decl
  ((tagged-sig-spec ...) unit-expr tagged-sig-spec))

unit/new-import-export와 같지만, define-unit처럼 unit-id에 정적 정보를 바인딩해요.

syntax

(unit/s
  (import tagged-sig-spec ...)
  (export tagged-sig-spec ...)
  init-depends-decl
  unit-id)

unit/new-import-export와 같지만, 연결 절이 추론되므로 unit-id가 적절한 정적 정보를 가져야 해요.

syntax

(define-unit/s name-id
  (import tagged-sig-spec ...)
  (export tagged-sig-spec ...)
  init-depends-decl
  unit-id)

unit/s와 같지만, define-unit처럼 name-id에 정적 정보를 바인딩해요.

더 알아보기