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);
or
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
type
TMyClass = class
private
FData: integer;
public
property Data: integer read FData write FData;
end;
I can write
type
TMyClass = class
public
property Data: integer;
end;
Another nice feature are property expressions – a way to write anonymous getters and setters.
type
TToDoListTemplate=class(TW3form)
public
property Task: string read (W3lblTask.Caption) write (W3lblTask.Caption);
property Detail: string read (W3lblDet.Caption) write (W3lblDet.Caption);
end;
These are incredible time saver. If I redo this the Delphi way, I end up with a much longer code.
type
TToDoListTemplate=class(TW3form)
private
function GetDetail: string;
function GetTask: string;
procedure SetDetail(value: string);
procedure SetTask(value: string);
public
property Task: string read GetTask write SetTask;
property Detail: string read GetDetail write SetDetail;
end;
function TToDoListTemplate.GetDetail: string;
begin
Result := W3lblDet.Caption;
end;
function TToDoListTemplate.GetTask: string;
begin
Result := W3lblTask.Caption;
end;
procedure TToDoListTemplate.SetDetail(value: string);
begin
W3lblDet.Caption := value;
end;
procedure TToDoListTemplate.SetTask(value: string);
begin
W3lblTask.Caption := value;
end;
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;
or
for var item in ActionList.Items do
When I have to write a multiline string constant, multiline strings come handy.
writeln("Hello
World");
Even better – DWS will ignore leading common indentation if string is introduced with #” (or #’).
writeln(#"
Hello
World");
And last (but definitely not least), DWS implements a true conditional operator.
s := if a = 0 then 0 else 1/a;