네임스페이스: 점으로 구분된 유닛

네임스페이스: 점으로 구분된 유닛

유닛 이름에는 점(dot)이 들어갈 수 있어요. 이 기능을 이용하면 유닛을 네임스페이스(namespace)처럼 계층적으로 조직할 수 있습니다.

출처: 문서

본문

유닛의 문법 도식에서 볼 수 있듯이, 유닛 이름은 점을 포함할 수 있어요. 즉 유닛을 네임스페이스로 조직할 수 있다는 뜻입니다. 다음은 올바른 유닛 선언이에요.

unit a.b;
interface
Function C : integer;
implementation
Function C : integer;
begin
  Result:=1;
end;
end.

이 유닛은 다음과 같이 사용할 수 있어요.

program d;
uses a.b;
begin
  Writeln(c);
end.

심볼 해석 규칙

심볼을 해석할 때, 유닛 스코프는 항상 유닛 내부의 심볼보다 우선합니다. 다음 유닛들이 있다고 할게요.

unit myunit;
interface
var
  test: record
    a: longint;
  end;
implementation
initialization
  test.a:=2;
end.

그리고

unit myunit.test;
interface
var
  a: longint;
implementation
initialization
  a:=1;
end.

다음 프로그램은 myunit.test.a를 유닛 myunit.test에 있는 변수 a로 해석해요.

uses
   myunit, myunit.test;
begin
  Writeln('myunit.test.a : ',myunit.test.a);
end.

그래서 다음을 출력합니다.

myunit.test.a : 1

유닛 순서를 바꿔도 이 결과는 변하지 않아요.

uses
   myunit.test, myunit;
begin
  Writeln('myunit.test.a : ',myunit.test.a);
end.

역시 다음을 출력하죠.

myunit.test.a : 1

마찬가지로, 다음 프로그램도 myunit.test.a를 유닛 myunit.test의 변수 a로 해석합니다.

uses
   myunit.test, myunit;
begin
  Writeln('a : ',a);
end.

이 경우 다음을 출력해요.

a : 1

반대로, 다음 프로그램은 test.a를 유닛 myunit에 있는 변수 test.a로 해석합니다.

uses
   myunit.test, myunit;
begin
  Writeln('test.a : ',test.a);
end.

이렇게 출력되죠.

test.a : 2

더 알아보기