Click here to Skip to main content
13,633,642 members
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

5K views
4 bookmarked
Posted 12 Jul 2018
Licenced CPOL

Delphi Language Progression Suggestions

Rate this:
Please Sign up or sign in to vote.
There are dozens of more things I'd like to see in Delphi, but here I will cover the ones that are easy to implement, yet provide large gains.

Since ~Delphi 2010, we have had generics, attributes, iterators, and some other compiler niceties. While many of the features we would like are difficult to implement, I believe that a few simple language features are long overdue.

There are dozens of more things I'd like to see in Delphi, but here I will cover the ones that are easy to implement, yet provide large gains.

Block Level Variable Declarations

Most compilers just declare block level variables internally as normal locals, i.e., it's just syntactic (but very useful!) sugar. It could even be done with a pre-processor but that would be hacky and affect the debugger.

The easiest way is scan for block vars. They internally treat them as locals. But add a prefix of suffix of otherwise unusable characters and give each block an ID.

So for example, give each block a number, 1, 2, 3, 4... then for each block var, internally just make it name$1 or something. Very easy to implement with very little impact to the compiler.

procedure Test();
begin
  for var c:string in List do begin
    ...
  end;

  var d:TStrings = TStringList.Create;
  var x:integer = 7; 

  for var f:integer := 1 to 10 do begin
    // Usable in block only. Not same as x declared before. 
    // Internally is just a normal function level var, ie as x$2 or something.
    var x:integer = 5;
    ...
  end;
end;

Type Inference

function Foo(): integer;
var
  x = 1;
  xList = TStringList.Create;
begin
  ...
  for var x in xList do begin
    ...
  end; 
end;

Using Statement

A try with an implicit finally free block.

Let me show you how this would alter existing code:

class function TMyWriter.ToString(aSrc: TRoot): string;
var
  xStream: TStringStream;
  xWriter: TWriter;
begin
  xWriter := TWriter.Create(aSrc); try
    xStream := TStringStream.Create; try
      xWriter.SaveToStream(xStream);
      Result := xStream.DataString;
    finally xStream.Free; end;
  finally xWriter.Free; end;
end;    

With using:

class function TMyWriter.ToString(aSrc: TRoot): string;
var
  xStream: TStringStream;
  xWriter: TWriter;
begin
  using var xWriter := TWriter.Create(aSrc) do begin
    using var xStream := TStringStream.Create do begin
      xWriter.SaveToStream(xStream);
      Result := xStream.DataString;
    end;
  end;
end;    

Auto Free

This, of course, applies to the Windows compiler, not the ARC based ones. Someday, the ARC compiler will reach Windows as well, but until it does, this solution is simple and helpful.

There are possible alternate syntaxes as well, but the main desire is for the functionality. The exact name of the keyword isn't that important either. Auto is simply shorter to type thatn autofree, dynamic, finalized, etc.

procedure Test2();
var
  x: integer;
  y: TStrings = TStringList.Create; auto;
begin
  ...
  auto z = TStringList.Create; 
  ...
  // At proc end, all .Free is called for all auto declared variables.
  // ie in this case, compiler emits a hidden y.Free and z.Free
end;

Let me show you how this would alter existing code:

class function TMyWriter.ToString(aSrc: TRoot): string;
var
  xStream: TStringStream;
  xWriter: TWriter;
begin
  xWriter := TWriter.Create(aSrc); try
    xStream := TStringStream.Create; try
      xWriter.SaveToStream(xStream);
      Result := xStream.DataString;
    finally xStream.Free; end;
  finally xWriter.Free; end;
end;    

With auto:

class function TMyWriter.ToString(aSrc: TRoot): string;
var
  xStream: TStringStream; auto;
  xWriter: TWriter; auto;
begin
  xWriter := TWriter.Create(aSrc);
  xStream := TStringStream.Create;
  xWriter.SaveToStream(xStream);
  Result := xStream.DataString;
end;    

Or when combined wtih block declared variables:

class function TMyWriter.ToString(aSrc: TRoot): string;
begin
  auto xWriter := TWriter.Create(aSrc);
  auto xStream := TStringStream.Create;
  xWriter.SaveToStream(xStream);
  Result := xStream.DataString;
end;    

Ternary Operator

It is quite hard to believe Delphi is over 20 years old and we still do not have a ternary operator. Syntax can vary, but the long used one among other langauges can be easily adapted to Delphi:

xValue := xCondition ? 'one' : 'two';

A commenter also suggested looking to Scala for a more Delphi like syntax that I liked as well:

xValue := if xCondition then 'one' else 'two';

Conclusion

With these simple additions, Delphi would be far easier to write code in without losing any functionality.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Chad Z. Hower, a.k.a. Kudzu
"Programming is an art form that fights back"

I am a former Microsoft Regional DPE (MEA) covering 85 countries, former Microsoft Regional Director, and 10 Year Microsoft MVP.

I have lived in Bulgaria, Canada, Cyprus, Switzerland, France, Jordan, Russia, Turkey, The Caribbean, and USA.

Creator of Indy, IntraWeb, COSMOS, X#, CrossTalk, and more.

You may also be interested in...

Pro

Comments and Discussions

 
PraiseGreat stuff, but my one wishlist item is missing Pin
Peter Turtle19-Jul-18 9:22
professionalPeter Turtle19-Jul-18 9:22 
GeneralRe: Great stuff, but my one wishlist item is missing Pin
Chad Z. Hower aka Kudzu20-Jul-18 3:09
memberChad Z. Hower aka Kudzu20-Jul-18 3:09 
QuestionMore Delphi Language Features Pin
Andrew Wozniewicz16-Jul-18 10:27
memberAndrew Wozniewicz16-Jul-18 10:27 
AnswerRe: More Delphi Language Features Pin
Chad Z. Hower aka Kudzu16-Jul-18 13:06
memberChad Z. Hower aka Kudzu16-Jul-18 13:06 
GeneralPlaying Devil's advocate... Pin
Member 1391249315-Jul-18 16:01
memberMember 1391249315-Jul-18 16:01 
GeneralRe: Playing Devil's advocate... Pin
Chad Z. Hower aka Kudzu16-Jul-18 4:09
memberChad Z. Hower aka Kudzu16-Jul-18 4:09 
GeneralRe: Playing Devil's advocate... Pin
Member 1391249316-Jul-18 5:13
memberMember 1391249316-Jul-18 5:13 
GeneralRe: Playing Devil's advocate... Pin
Chad Z. Hower aka Kudzu16-Jul-18 5:17
memberChad Z. Hower aka Kudzu16-Jul-18 5:17 
GeneralMessage Closed Pin
16-Jul-18 6:15
memberMember 1391249316-Jul-18 6:15 
GeneralRe: Playing Devil's advocate... Pin
Chad Z. Hower aka Kudzu16-Jul-18 6:29
memberChad Z. Hower aka Kudzu16-Jul-18 6:29 
QuestionThe first and main suggestion is: Pin
Member 289377914-Jul-18 18:38
memberMember 289377914-Jul-18 18:38 
AnswerRe: The first and main suggestion is: Pin
Chad Z. Hower aka Kudzu19-Jul-18 3:13
memberChad Z. Hower aka Kudzu19-Jul-18 3:13 
GeneralRe: The first and main suggestion is: Pin
Member 289377919-Jul-18 6:59
memberMember 289377919-Jul-18 6:59 
QuestionNice Article, Glad to hear ANYTHING about Delphi, LOL Pin
Kirk 1038982113-Jul-18 7:33
memberKirk 1038982113-Jul-18 7:33 
AnswerRe: Nice Article, Glad to hear ANYTHING about Delphi, LOL Pin
Chad Z. Hower aka Kudzu13-Jul-18 7:49
memberChad Z. Hower aka Kudzu13-Jul-18 7:49 
AnswerRe: Nice Article, Glad to hear ANYTHING about Delphi, LOL Pin
kother14-Jul-18 6:15
memberkother14-Jul-18 6:15 
QuestionDelphi is not C Pin
Gerhard Eichhorn13-Jul-18 6:46
memberGerhard Eichhorn13-Jul-18 6:46 
GeneralInference and auto release Pin
Olivier Sannier13-Jul-18 3:47
memberOlivier Sannier13-Jul-18 3:47 
GeneralRe: Inference and auto release Pin
Chad Z. Hower aka Kudzu13-Jul-18 6:53
memberChad Z. Hower aka Kudzu13-Jul-18 6:53 
GeneralRe: Inference and auto release Pin
Chad Z. Hower aka Kudzu13-Jul-18 7:47
memberChad Z. Hower aka Kudzu13-Jul-18 7:47 
GeneralRe: Inference and auto release Pin
Olivier Sannier15-Jul-18 22:48
memberOlivier Sannier15-Jul-18 22:48 
GeneralRe: Inference and auto release Pin
Chad Z. Hower aka Kudzu16-Jul-18 4:31
memberChad Z. Hower aka Kudzu16-Jul-18 4:31 
AnswerRisks of this suggestions for a team integrating a young developer Pin
Member 1391014212-Jul-18 23:16
memberMember 1391014212-Jul-18 23:16 
GeneralRe: Risks of this suggestions for a team integrating a young developer Pin
Chad Z. Hower aka Kudzu13-Jul-18 9:03
memberChad Z. Hower aka Kudzu13-Jul-18 9:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04-2016 | 2.8.180712.1 | Last Updated 12 Jul 2018
Article Copyright 2018 by Chad Z. Hower aka Kudzu
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid