Tiburon - new language features for Delphi 2009

Posted by on in Blogs
There are loads of new features in Tiburon for Delphi and C++Builder developers. New language features, more compatibility at the component level between Delphi and C++, C++0x draft standard language and library support, new and enhanced VCL components, and a totally revamped DataSnap.

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<T> = class
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<Integer>;
slist: TList<String>;

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.

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/
About
Gold User, Rank: 1, Points: 2466
David Intersimone (known to many as David I.) is a passionate and innovative software industry veteran-often referred to as a developer icon-who extols and educates the world on Embarcadero developer tools. He shares his visions as an active member of the industry speaking circuit and is tapped as an expert source by the media. He is a long-standing champion of architects, developers and database professionals and works to ensure that their needs are folded into Embarcadero's strategic product plans. David holds a bachelor's degree in computer science from California Polytechnic State University at San Luis Obispo, California.
Comments are not available for public users. Please login first to view / add comments.