Tiburon - new language features for Delphi 2009(有关泛型的)

Tiburon - new language features for Delphi 2009

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/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值