|
|
(97 intermediate revisions by 3 users not shown) |
Line 1: |
Line 1: |
− | == Fundamental syntax changes ==
| |
| | | |
− | Delphi and Pascal comes from another language family than D.
| |
− |
| |
− | == Types equivalence ==
| |
− |
| |
− | {| border="1" cellspacing="0"
| |
− | | '''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 inherithence of classes is not allowed but can be achieved with interfaces.
| |
− |
| |
− | Delphi, Pascal
| |
− |
| |
− | <pre>type ISome = interfaca
| |
− | procedure blah();
| |
− | end;
| |
− |
| |
− | type TThing = class
| |
− | end;
| |
− |
| |
− | type TSomething = classs(TThing, ISome)
| |
− | procedure blah();
| |
− | end;</pre>
| |
− | D2
| |
− |
| |
− | <pre>interface ISome(){
| |
− | void blah();
| |
− | }
| |
− |
| |
− | class TThing{
| |
− | }
| |
− |
| |
− | class TSomething: TThing, ISome{
| |
− | void blah(){}
| |
− | }</pre>
| |
− | == Templates ==
| |
− |
| |
− | 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 instantciation of a template).
| |
− |
| |
− | D uses parens for the declaration and the ! symbol for the instantiation. Parens are used after the ! if the template expect several parameters.
| |
− |
| |
− | * Delphi or FPC with the dialect ''{$Mode Delphi}''
| |
− |
| |
− | <pre>type foo<T,G,A> = class
| |
− | end;
| |
− |
| |
− | type fooccc = class(foo<char, char, char>)
| |
− | end;
| |
− |
| |
− | var
| |
− | bar: foo<integer,single,double>;</pre>
| |
− | * FPC with the dialect ''{$Mode objfpc}'' (aka '''''modern''''' Pascal)
| |
− |
| |
− | In the Pascal tradition, the objfpc syntax recquires two explicit keywords for the template declaration and the template instantiation: '''generic''' and '''specialize'''.
| |
− |
| |
− | * D
| |
− |
| |
− | <pre>class foo(T,G,A){
| |
− | }
| |
− |
| |
− | alias fooccc = foo!(char, char, char);
| |
− |
| |
− | foo!(int, float, double) bar;</pre>
| |
− |
| |
− | == Conditional compilation ==
| |
− |
| |
− | D conditional compilation is one of the unique feature of the lanquage. While in Pascal you used a custom define like this:
| |
− |
| |
− | <pre>a := 8 shr 3;
| |
− | {$IFDEF MYSWITCH}
| |
− | // some conditionally compiled code
| |
− | {$ENDIF}</pre>
| |
− | in D the equivalent use a standard language construct '''''version(argument)''''':
| |
− |
| |
− | <pre>a =>> 3;
| |
− | version(MYSWITCH)
| |
− | {
| |
− | // some conditionally compiled code
| |
− | }</pre>
| |
− | 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 instanciated.
| |
− |
| |
− | <pre>struct foo(T)
| |
− | {
| |
− | // something is only compiled if T type is string.
| |
− | static if (is(T == string)){
| |
− | void something(){}
| |
− | }
| |
− |
| |
− | }</pre>
| |
− | 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.
| |
− |
| |
− | == inclusion ==
| |
− |
| |
− | D has a feature similar to Pascal/delphi source inclusion.
| |
− |
| |
− | for the example the following inclusion:
| |
− |
| |
− | <pre>procedure Something;
| |
− | begin
| |
− | {$IFDEF DEMO}
| |
− | {$I myDemoImplementation.inc}
| |
− | {$ELSE}
| |
− | {$I myFullImplementation.inc}
| |
− | {$ENDIF}
| |
− | end;</pre>
| |
− | is rewritten in D
| |
− |
| |
− | <pre>void something()
| |
− | {
| |
− | static enum demoCode = import(myDemoImplementation.d);
| |
− | static enum fullCode = import(myFullImplementation.d);
| |
− | version(DEMO){
| |
− | mixin(demoCode);
| |
− | } else {
| |
− | mixin(fullCode);
| |
− | }
| |
− | }</pre>
| |