더 쉬운코드, 더 유연한 코드를 작성할 수 있는 현대식 문법
현대식 문법
현대식 문법으로 여러분의 코드를 더 짧게, 더 쉽게, 더 유연하게 작성할 수 있습니다.
아래 간단한 문법소개와 샘플을 통해 어떤 기능을 하는지 소개합니다. 하지만, 여러분의 코드에 적용하기 위해서는 더 깊은 이해가 필요합니다. 엠바카데로 기술문서와 현대식 문법을 더 자세히 익힐 수 있는 도서를 소개해 드리니 참고하셔서 더 다양한 문법으로 더 강력한 코드를 작성해보시기 바랍니다.
11월 13일(서울), 11월 20일(대구)에 열리는 "VCL 애플리케이션을 더욱 멋지게 'Developer Direct LIVE!'" 세미나에서도 해당 내용을 직접 들어볼 수 있으니 신청하셔서 직접 들어보시다면 더 도움이 될것입니다.
❑ 타입헬퍼(Type Helper)
타입헬퍼는 클래스, 레코드, 열거형, 타입의 기능을 확장할 수 있는 문법입니다.
아래 예제와 같이 열거형의 기능을 확장하고 기존 TPicture 클래스의 기능을 확장하는 방식으로 활용할 수 있습니다.
type
TGeoDirection = (North, East, South, West);
// 열거형을 만자열로 변환하는 기능 추가
TGeoDirectionHelper = record helper for TGeoDirection
function ToString: string; inline;
end;
// URL 이미지를 로드하는 기능 추가
TPictureHelper = class helper for TPicture
public
procedure LoadFromUrl(AUrl: string);
procedure LoadFromUrlWithThread(AUrl: string);
end;
function TGeoDirection.ToString: string;
begin
case Self of
TGeoDirection.North: Result := '북쪽 (N)';
TGeoDirection.East: Result := '동쪽 (E)';
TGeoDirection.South: Result := '남쪽 (S)';
TGeoDirection.West: Result := '서쪽 (W)';
else
raise Exception.Create('Unknown "TGeoDirection" value');
end;
end;
var
Direction: TGeoDirection;
begin
Direction := South;
ShowMessage(Direction.ToString);
Image1.Picture.LoadFromUrl('http://abc.com/Image.jpg');
end;더 알아보기
❑ 제너릭(Generic)
클래스와 메소드에서 사용하는 데이터 타입을 사전에 정하지 않고 유연하게 사용할 수 있는 문법입니다.
구현 시 데이터 타입을 지정해 사용할 수 있어 하나의 클래스(메소드)를 다양한 데이터 타입으로 사용할 수 있습니다.
아래의 예제에서는 전통적인 클래스(TClassicSIPaire) 형식은 타입을 지정해 클래스를 만들고,
제너릭 클래스의 경우 TKey, TValue 두개의 타입인자를 통해 사전에 데이터 타입을 정하지 않아, 재선언(TSIPaire) 해 사용하거나 구현 시 데이터 타입 지정(KeyValue3: TPair<string, Integer>)하는 등 유연하게 사용할 수 있습니다.
type
TClassicSIPair = class
private
FKey: String;
FValue: Integer;
public
function GetKey: String;
procedure SetKey(Key: String);
function GetValue: Integer;
procedure SetValue(Value: Integer);
property Key: string read GetKey write SetKey;
property Value: Integer read GetValue write SetValue;
end;
TPair<TKey,TValue> = class
private
FKey: TKey;
FValue: TValue;
public
function GetKey: TKey;
procedure SetKey(Key: TKey);
function GetValue: TValue;
procedure SetValue(Value: TValue);
property Key: TKey read GetKey write SetKey;
property Value: TValue read GetValue write SetValue;
end;
TSIPair = TPair<string, Intege>;
TSSPair = TPair<string, string>;
procedure TForm1.Button1Click(Sender: TObject);
var
KeyValue1: TClassicSIPair;
KeyValue2: TSIPair;
KeyValue3: TPair<string, Intege>;
I: Integer;
ListUser1: TObjectList;
ListUser2: TObjectList<TUserData>;
begin
KeyValue1 := TClassicSIPair.Create; // 전통적인 클래스
KeyValue2 := TSIPair.Create; // 닫힌 생성자형식 제너릭
KeyValue3 := TPair<string, Intege>.Create; // 열린 생성자형식 제너릭
KeyValue1.Key := '사원번호';
KeyValue1.Value := 123456;
KeyValue2.Key := '사원번호';
KeyValue3.Key := '사원번호';
// 만약? Value 가 Integer에서 string으로 변경된다면? TClassicSSPaire 클래스를 새로 생성해야 함
KeyValue1.Free;
KeyValue2.Free;
KeyValue3.Free;
// 중략
for I := 0 to ListUser1.Count - 1 do
begin
Obj := ListUser1[I];
User := TUserData(Obj);
Log(User.ToString);
end;
for User in ListUser2 do
Log(User.ToString);
// ListUser2.Items[0].ToString
end;더 알아보기
- 엠바카데로 도움말
- 데브기어 기술문서
❑ 익명 메소드(Anonymous Method)
메소드(procedure, function)를 변수와 파라메터 형태로 사용할 수 있는 문법입니다.
아래 예제는 3초 후에 메시지를 표시하는 기능을 익명 메소드를 통해 구현했습니다. 익명 메소드를 사용하면 원하는 로직을 변수에 저장 후 원하는 시점에 호출하는 방식등으로 사용할 수 있습니다.
// 지정된 시간 이후에 파라메터의 메소드를 실행
procedure DelayProc(ADelay: Integer; AProc: TProc);
begin
Sleep(ADelay);
AProc;
end;
var
Noti: TProc;
StrToIntFunc: TFunc<string, Integer>;
begin
DelayProc(3000, procedure
begin
ShowMessage('Delay Message');
end);
Noti := procedure
begin
ShowMessage('Anonymous Method');
end;
Noti;
StrToIntFunc := function(AStr: string): Integer
begin
Result := StrToIntDef(AStr, 0);
end;
ShowMessage(StrToIntFunc('100').ToString);
end;더 알아보기
- 엠바카데로 도움말
- 데브기어 기술문서