Here is a quick look at two of the new Delphi language features - Generics and Delphi Anonymous Methods
Delphi Generics: Great for containers and collections
Declaration: TList
private
FItems: array of T;
FCount: Integer;
procedure Grow(ACapacity: Integer);
function GetItem(AIndex: Integer): T;
procedure SetItem(AIndex: Integer; AValue: T);
public
procedure Add(const AItem: T);
procedure AddRange(const AItems: array of T);
procedure RemoveAt(AIndex: Integer);
procedure Clear;
property Item[AIndex: Integer]: T
read GetItem write SetItem; default;
property Count: Integer read FCount;
end;
Use:var
ilist: TList
slist: TList
procedure PrintListInteger;
var
i: Integer;
begin
for i := 0 to ilist.Count - 1 do
Write(ilist[i], ' ');
Writeln;
end;
procedure PrintListString;
var
i: Integer;
begin
for i := 0 to slist.Count - 1 do
Write(slist[i], ' ');
Writeln;
end;
begin
ilist := TList.Create;
try
ilist.AddRange([1, 2, 3]); // ['1', 'second', 'third']);
PrintListInteger;
ilist.RemoveAt(1);
PrintListInteger;
ilist.Clear;
PrintListInteger;
finally
ilist.Free;
end;
slist := TList.Create;
try
slist.AddRange(['one', 'two', 'three']); // ['first', 'second', 'third']);
PrintListString;
slist.RemoveAt(1);
PrintListString;
slist.Clear;
PrintListString;
finally
slist.Free;
end;
Readln;
end.
The Tiburon Generics.Collections unit includes: TList, TQueue, TStack, TDictionary, TObjectList, TObjectQueue, TObjectStack, and TObjectDictionary.
Delphi Anonymous Methods: use them when nothing else is nearly as good, ideal for passing code when you need to parameterize types and procedures by code or behaviour. You can also "simulate" new syntax constructs defined entirely in libraries. Don’t use them when for/in or an equivalent loop would do.
Declaration:type
// method reference
TProc = reference to procedure(x: Integer);
procedure Call(const proc: TProc);
begin
proc(42);
end;
Use:var
proc: TProc;
begin
// anonymous method
proc := procedure(a: Integer)
begin
Writeln(a);
end;
Call(proc);
readln
end.
Source : http://blogs.codegear.com
For a look at the new language construct for Exit, check out Nick Hodges blog at http://blogs.codegear.com/nickhodges/2008/07/22/39079/
No comments:
Post a Comment