While I love the way Delphi is expanding to multiple platforms (but please, guys, stop the horses once in a while and fix some bugs!), I think the Delphi language could use a facelift. In particular, I like what Eric is doing with the DWS and I absolutely enjoy coding in Smart Mobile Studio where I could use the full power of Eric’s improvements to the language.
I absolutely love DWS’ lambda statement. Delphi’s approach to anonymous methods is so damn long-winded (even for us Pascal programmers who love to type) that lots of programmers reject it just because of the unwieldy syntax. In DWS, however, I can use
var repeater := TW3EventRepeater.Create(lambda (Sender) => MyFunction, 5000);
ResizeChildren(FClientArea.Height, [TAlign.Top, TAlign.Bottom],
lambda (layout) => layout.GetConfig.GetHeight,
lambda (layout, value) layout.GetConfig.Height(value) end);
Much shorter. More readable.
Also useful are properties with anonymous storage. If my property only exposes a field and doesn’t use getter or setter, I can shorten the code by 50% by not defining the field at all. Instead of
TMyClass = class
property Data: integer read FData write FData;
I can write
TMyClass = class
property Data: integer;
Another nice feature are property expressions – a way to write anonymous getters and setters.
property Task: string read (W3lblTask.Caption) write (W3lblTask.Caption);
property Detail: string read (W3lblDet.Caption) write (W3lblDet.Caption);
These are incredible time saver. If I redo this the Delphi way, I end up with a much longer code.
function GetDetail: string;
function GetTask: string;
procedure SetDetail(value: string);
procedure SetTask(value: string);
property Task: string read GetTask write SetTask;
property Detail: string read GetDetail write SetDetail;
function TToDoListTemplate.GetDetail: string;
Result := W3lblDet.Caption;
function TToDoListTemplate.GetTask: string;
Result := W3lblTask.Caption;
procedure TToDoListTemplate.SetDetail(value: string);
W3lblDet.Caption := value;
procedure TToDoListTemplate.SetTask(value: string);
W3lblTask.Caption := value;
I also love type inference and in-line variable declaration which allow me to write code like this:
var item := ActionList.Items[ActionList.Add] as TLBItem;
item.Header := header;
item.Text := text;
for var item in ActionList.Items do
When I have to write a multiline string constant, multiline strings come handy.
Even better – DWS will ignore leading common indentation if string is introduced with #” (or #’).
And last (but definitely not least), DWS implements a true conditional operator.
s := if a = 0 then 0 else 1/a;