Jump to content
vfbb

Undocumented language enhancements

Recommended Posts

Sometimes I come across some delphi language enhancements that are undocumented or are poorly documented. In the last few years docwiki has improved a lot, but there are still undocumented cases, such as accessing an item in an array just by declaring a property, without any method:

  TipGeolocation = record
  private
    FCoordinates: array[0..1] of Double;
  public
    property Latitude: Double read FCoordinates[0] write FCoordinates[0];
    property Longitude: Double read FCoordinates[1] write FCoordinates[1];
  end;

Share here any enhancements to the delphi language that you have discovered but are not documented.

  • Like 4

Share this post


Link to post

Virtual interfaces: to create an object that support an interface in run time. See System.Rtti.TVirtualInterface

Share this post


Link to post
49 minutes ago, vfbb said:

Share here any enhancements to the delphi language that you have discovered but are not documented.

The entire language is essentially undocumented. There is no formal specification, no documented grammar etc. 

  • Haha 2

Share this post


Link to post
13 hours ago, vfbb said:

such as accessing an item in an array just by declaring a property, without any method

 

Similarly, should work with records:

type
  TMy = class
  private
    FPoint: TPoint;
  public
    property X: Integer read FPoint.X write FPoint.X;
    property Y: Integer read FPoint.Y write FPoint.Y;
  end;

 

This can even be mixed with static arrays:

type
  TMyData = record
    Points: array[0..1] of TPoint;
  end;

  TMy = class
  private
    FData: TMyData;
  public
    property X: Integer read FData.Points[1].X write FData.Points[1].X;
    property Y: Integer read FData.Points[1].Y write FData.Points[1].Y;
  end;

 

 

Edited by balabuev
  • Like 2

Share this post


Link to post
8 minutes ago, Der schöne Günther said:

Not highly scientific, but better than nothing:

Delphi Language Reference - RAD Studio (embarcadero.com)

Obviously I know this and have read it plenty of times. But it's way short of a formal specification. Just read the topic for properties. What does fieldOrMethod mean? It's not precisely defined anywhere.

 

The entire language documentation is like that. Just a little imprecise. 

Edited by David Heffernan

Share this post


Link to post

"Static variables" inside procedures

function Counter: Integer;
{$J+}
const
  I: Integer = 0;
{$J-}
begin
  I := I + 1;
  Result := I;
end;

When you declare a const with a type, Delphi allocates memory for the constant. The program can change the value of the “constant,” just as though it were a variable. The difference between a typed constant and a local variable is that a typed constant in a subroutine keeps its value across subroutine calls.

If you disable the $WriteableConst directive, you can prevent any assignments to a typed constant, thereby making the constant truly constant. There is no substitute for typed constants in a subroutine, though, so disable $WriteableConst with care. Disable only specific constants whose value you want to protect, and do not disable this directive globally in a project or an entire file.

 

Short init of record constants

 

type 
  TR = record
    a: string;
    b: string;
  end;

Const TR_Empty: TR = ();
Const TR_A: TR = (a: 'foo');

 

(not undocumented but non-obvious ones)

Edited by Fr0sT.Brutal
  • Like 4

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×