Difference between revisions of "Programming in D for Delphi Programmers"

From D Wiki
Jump to: navigation, search
m (RTTI, published attribute and properties.)
m (Switching)
Line 1: Line 1:
== Switching ==
== Introduction ==
Delphi and Pascal come from another language family than D. D uses the C syntax. In short, a C syntax uses more symbols and more operators.
Delphi and Pascal come from another language family than D. D uses the C syntax. In short, a C syntax uses more symbols and more operators.

Revision as of 13:45, 5 November 2014


Delphi and Pascal come from another language family than D. D uses the C syntax. In short, a C syntax uses more symbols and more operators. The following document develops a few points over which D and Delphi/Pascal have some similarities or ambiguities.

Types equivalence

Pascal D notes
Pointer void*
Byte ubyte 8 bit unsigned integer. note the ambiguity when coming from Pascal.
ShortInt byte 8 bit signed integer
Word uint16 16 bit unsigned integer
SmallInt int16 16 bit signed integer
DWord / Cardinal uint 32 bit unsigned integer
Integer int 32 bit signed integer
UInt64 ulong 64 bit unsigned integer
Int64 long 64 bit signed integer
Single float
Double double
NativeUInt size_t in both case this is either an alias to a unsigned 32 bit integer or to a unsigned 64 bit integer
NativeInt ptrdiff_t in both case this is either an alias to a signed 32 bit integer or to a signed 64 bit integer

The object model

The object model is striclty equivalent. Multiple inheritence of classes is not allowed but can be achieved with interfaces.

  • Delphi, Pascal
type  ISome = interfaca
    procedure blah();

type TThing = class

type  TSomething = classs(TThing, ISome)
  procedure blah();
  • The D2 way:
interface ISome(){
    void blah();

class TThing{

class TSomething: TThing, ISome{
    void blah(){}


Traditional Delphi and Pascal didn’t supported templates. Delphi supports them since D2009, using the most common syntax: the left/right angle brackets (declaration and instantiation of a template).

D uses parens for the declaration and the exclamation mark (!) for the instantiation. Parens are used after the ! if the template expects several parameters.

  • Delphi or FPC with the dialect {$Mode Delphi}
type foo<T,G,A> = class
type fooccc = class(foo<char, char, char>)
  bar: foo<integer,single,double>;
  • FPC with the dialect {$Mode objfpc} (aka object Pascal)

Faithfully to the Pascal tradition, the objfpc dialect recquires two explicit keywords for the template declaration and the template instantiation: generic and specialize:

type generic foo<T,G,A> = class

type fooccc = class(specialize foo<char, char, char>)

  bar: specialize foo<integer,single,double>;
  • the D2 way:
class foo(T,G,A){
class fooccc: foo!(char, char, char){
foo!(int, float, double) bar;

Conditional compilation

D conditional compilation is one of the unique feature of the lanquage. While in Pascal you used a custom definition like this:

a := a shr 3;
  // some conditionally compiled code

in D the equivalent use a standard language construct version(Identifier):

a =>> 3;
    // some conditionally compiled code   

But now you’ll get more. A feature that doesn’t exist in Pascal and Delphi is the static if expression. It allows a more advanced conditional compilation, particularly in the templates, since it’s allow to test the type of a template parameters when it gets instantiated.

struct foo(T)
    // something is only compiled if T type is string.
    static if (is(T == string)){
    void something(){}

This is also related to the template constraints (the constraint is applied to the whole template while in our previous example it’s only applied to the enclosed expressions). You might know the principle if you’ve used Delphi XE6 or upper.


D has a feature similar to Pascal/delphi source inclusion.

for the example the following inclusion:

procedure Something;
  {$I myDemoImplementation.inc}
  {$I myFullImplementation.inc}

is rewritten in D

void something()
    static enum demoCode = import(myDemoImplementation.d);
    static enum fullCode = import(myFullImplementation.d);
    } else {

RTTI, published attribute and properties.

In Pascal and Delphi, the Runtime Type Informations (RTTI) are an important feature used by the TPersistent and the TComponent system, as well as for custom serialization or for software settings or even Object dumping into a database.

D has no equivalent feature. The published attribute does not exist but instead you have some compile-time reflection (traits, std.traits, user defined attributes) which could be used to design a similar system.

Pascal properties are deeply linked to the RTTI mechanisms. D properties are a bit different and even more powerfull. They allow to use the assign operator instead of calling the setter with parens (so far this is a common behaviour) but they also can be overloaded.

A classic, non published, Pascal property (interface section only):

  FField: integer;
  procedure SetField(AValue: Integer);
  property Field: integer read FField write SetField;

The D equivalent, with overload:

    int fField;
    @property void field(int aValue){fField = aValue;}
    @property void field(string aValue){fField = to!int(aValue); }
    @property int field(){return fField;}