Comparison of Pascal and Delphi: Difference between revisions

Content deleted Content added
Citation bot (talk | contribs)
Add: title. Changed bare reference to CS1/2. | Use this bot. Report bugs. | Suggested by BrownHairedGirl | Linked from User:BrownHairedGirl/Articles_with_bare_links | #UCB_webform_linked 472/998
more structure content
Line 7:
The Borland dialect, like the popular [[UCSD Pascal]] before it, took the 'P4' version of the language as its basis, rather than Wirth's final revision. After much evolution independent of Standard Pascal, the Borland variant became the basis for Delphi. This page goes over the differences between Delphi and Standard Pascal. It does <em>not</em> go into Delphi-specific extensions to the language, which are numerous and still increasing.
 
== Exclusive features ==
== Differences between Standard Pascal and Delphi ==
Following features are mutually exclusive.
The Standard Pascal implementation is not accepted by Delphi and vice versa, the Delphi code is not acceptable in Standard Pascal.
 
=== Nested comments ===
1. The way procedures and functions can be passed as parameters differs: Delphi requires explicit procedural types to be declared where Standard Pascal does not.
Standard Pascal requires that the comment delimiters <syntaxhighlight lang="pascal" inline>{</syntaxhighlight> and the bigramm <syntaxhighlight lang="pascal" inline>(*</syntaxhighlight>, as well as <syntaxhighlight lang="pascal" inline>}</syntaxhighlight> and <syntaxhighlight lang="pascal" inline>*)</syntaxhighlight> are synonymous to each other.
In Delphi, however, a block comment started by <syntaxhighlight lang="pascal" inline>{</syntaxhighlight> ''must'' be closed with a <syntaxhighlight lang="pascal" inline>}</syntaxhighlight>.
The bigramm <syntaxhighlight lang="pascal" inline>*)</syntaxhighlight> will only close any comment that started with <syntaxhighlight lang="pascal" inline>(*</syntaxhighlight>.
This scheme allows for ''nested'' comments at the expense of compiler complexity.
 
=== Procedural data types ===
2. Delphi does not support so called 'extraprocedural gotos', in which [[goto]] statements may reference targets outside the current procedure/function body.
The way procedures and functions can be passed as parameters differs:
Delphi requires explicit procedural types to be declared where Standard Pascal does not.
{| class="wikitable" style="margin: auto;"
|+ comparison procedural data types
|-
! Standard Pascal !! Delphi
|-
| <syntaxhighlight lang="pascal" highlight="3">program proceduralDataType(output);
{ `printYIntersect` has one procedural parameter: }
procedure printYIntersect(function f(x: real): real);
begin
writeLn(f(0.0));
end;
{ Standard Pascal does not have procedural “pointers.” }
function f(x: real): real;
begin
f := cos(x);
end;
{ ─── MAIN ───────────────────────────────────────────── }
begin
printYIntersect(f);
end.</syntaxhighlight>
| <syntaxhighlight lang="delphi" highlight="1-4">type
TFunc = function(x: real): real;
 
procedure printYIntersect(f: TFunc);
3. Standard Pascal has file 'buffer variables', together with 'get' and 'put' standard procedures that operate on them. Neither are supported in Delphi.
begin
writeLn(f(0.0));
end;
 
function f(x: real): real;
4. Given a variant record in Standard Pascal, the size of a particular variant can be specified. Delphi does not support this form of 'sized' dynamic variable allocation:
begin
f := cos(x);
end;
// ─── MAIN ─────────────────────────────────────────────
begin
printYIntersect(@f);
end.</syntaxhighlight>
|}
 
=== Conversion of newline characters ===
<syntaxhighlight lang="pascal">
Various computer systems show a wide variety how to indicate a [[newline]].
new(p, t) //where t is a variant record tag type; does not compile in Delphi
This affects the internal representation of <syntaxhighlight lang="pascal" inline>text</syntaxhighlight> files which are composed of a series of “lines”.
</syntaxhighlight>
In order to relieve the programmer from any associated headaches, Standard Pascal mandates that ''reading'' an “end-of-line character” returns a single space character.
To distinguish such an “end-of-line” space character from a space character that is actually genuine payload of the line, <syntaxhighlight lang="pascal" inline>EOLn</syntaxhighlight> becomes <syntaxhighlight lang="pascal" inline>true</syntaxhighlight>.
 
Delphi does not show this behavior.
5. The functions 'pack' and 'unpack' are not implemented in Delphi.
Reading a newline will return whatever character sequence represents a newline on the current host system, for example two <syntaxhighlight lang="delphi" inline>char</syntaxhighlight> values <syntaxhighlight lang="delphi" inline>chr(13)</syntaxhighlight> (carriage return) plus <syntaxhighlight lang="delphi" inline>chr(10)</syntaxhighlight> (line feed).
 
== Additional or missing features ==
6. The Delphi compiler does not treat { and (*, } and *) as synonyms like Standard Pascal requires. In other words,
Following features are present or missing in either language.
 
=== Global <syntaxhighlight lang="delphipascal" inline>goto</syntaxhighlight> ===
Standard Pascal permits a <syntaxhighlight lang="pascal" inline>goto</syntaxhighlight> to any <syntaxhighlight lang="pascal" inline>label</syntaxhighlight> defined in scope.
{ comment *)
In Delphi a <syntaxhighlight lang="delphi" inline>goto</syntaxhighlight> must be within the current routine, i. e. may not leave the <syntaxhighlight lang="delphi" inline>begin … end</syntaxhighlight>-frame.
</syntaxhighlight>
<syntaxhighlight lang="pascal" highlight="6,7">program jumpAround;
label
999;
procedure foo;
begin
{ This is not permitted in Delphi: }
goto 999;
end;
begin
foo;
999: ;
end.</syntaxhighlight>
 
=== Buffer variables ===
is not valid in Delphi. Instead, Delphi uses the scheme of allowing the different comment types to indicate nested comments.
Delphi does not support buffer variables and associated standard routines <syntaxhighlight lang="pascal" inline>get</syntaxhighlight> and <syntaxhighlight lang="pascal" inline>put</syntaxhighlight>.
<syntaxhighlight lang="pascal" highlight="5,6,15,19">program copy(input, output);
begin
while not EOF(input) do
begin
{ Copy file buffers. Not supported by Delphi }
output↑ := input↑;
{ Input↑ contains a space if a new-line occurred. }
if EOLn(input) then
begin
writeLn(output);
end
else
begin
put(output);
end;
{ Advance reading cursor. }
get(input);
end;
end.</syntaxhighlight>
 
=== Discriminated variant record allocation ===
7. Delphi does not replace eoln with space as the Pascal standard requires. When reading through the end of a line, the eoln character is supposed to be replaced with a space in ISO 7185. Instead, reading through eoln in Delphi gives the character code for carriage return (13), followed by line feed (10).
In Standard Pascal allocating memory for a variant <syntaxhighlight lang="pascal" inline>record</syntaxhighlight> may indicate a specific variant.
This allows implementations to allocate the least amount of ''really'' necessary memory.
Delphi does not support this.
<syntaxhighlight lang="pascal" highlight="13,14">program variantRecord;
type
sex = (female, male);
clothingMeasures = record
girth: real;
case gender: sex of
female: (underbust: real);
male: ( );
end;
var
size: clothingMeasures;
begin
{ NB: No space allocated for `underbust`. }
new(size, male);
end.</syntaxhighlight>
 
=== Temporary files ===
8. Numbers and booleans are not printed out in their 'default' field widths by Delphi's version of the Write and WriteLn standard procedures, being instead printed in the minimum amount of space. For example, in Delphi,
In Delphi any file must be backed by a file in the file system.
That means any <syntaxhighlight lang="delphi" inline>file</syntaxhighlight> needs to be associated with a file name with Delphi’s <syntaxhighlight lang="delphi" inline>assign</syntaxhighlight> procedure.
In contrast, Standard Pascal is usable without file names.
The following will produce a run-time error with Delphi.
<syntaxhighlight lang="pascal">program temporaryFile(output);
var
FD: text;
begin
rewrite(FD);
writeLn(FD, 'Hello world!');
end.</syntaxhighlight>
 
=== Packing ===
<syntaxhighlight lang="delphi">
Delphi does not implement the standard procedures <syntaxhighlight lang="pascal" inline>pack</syntaxhighlight> and <syntaxhighlight lang="pascal" inline>unpack</syntaxhighlight>.
write(5);
Regardless, transferring data between packed and unpacked data types is an easy feat, although the implementation might not be as efficient as a compiler vendor supplied implementation would be.
write(55);
</syntaxhighlight>
 
=== Missing default <syntaxhighlight lang="pascal" inline>write</syntaxhighlight> width ===
is equivalent to:
Delphi does not associate the data type <syntaxhighlight lang="pascal" inline>Boolean</syntaxhighlight> with a default width if specified as <syntaxhighlight lang="pascal" inline>write</syntaxhighlight>/<syntaxhighlight lang="pascal" inline>writeLn</syntaxhighlight> parameters.
Delphi demonstratets the behavior as usual for character-strings.
 
=== Overloading ===
<syntaxhighlight lang="pascal">
Delphi permits overloading routines.
write(5:1);
In Standard Pascal identifiers must be unique in every block.
write(55:2);
</syntaxhighlight lang="delphi">function f(x: integer): real;
begin
result := sin(x);
end;
 
function f(x: real): real;
However, Standard Pascal requires it to be equivalent to the following (TotalWidth is implementation-defined):
begin
result := cos(x);
end;
// ─── MAIN ─────────────────────────────────────────────
begin
// Note the different data types.
writeLn(f(3));
writeLn(f(3.0));
end.</syntaxhighlight>
 
=== Default parameter values ===
<syntaxhighlight lang="pascal">
Delphi permits [[default parameter]]s.
write(5:TotalWidth);
write(55:TotalWidth);
</syntaxhighlight>
 
== Peculiar implementation characteristics ==
Similarly, for booleans,
=== Standard <syntaxhighlight lang="pascal" inline>write</syntaxhighlight> width ===
 
In Pascal, if the destination file is a <syntaxhighlight lang="pascal" inline>text</syntaxhighlight> file, the parameters to <syntaxhighlight lang="pascal" inline>write</syntaxhighlight>/<syntaxhighlight lang="pascal" inline>writeLn</syntaxhighlight> have an implemention-defined default total width.
<syntaxhighlight lang="pascal">
In Delphi, for <syntaxhighlight lang="delphi" inline>integer</syntaxhighlight> values this is simply <syntaxhighlight lang="delphi" inline>1</syntaxhighlight>.
write(false);
That means always the least amount of space is occupied.
write(true);
Other compilers have shown default widths of, for example, <syntaxhighlight lang="pascal" inline>20</syntaxhighlight> allowing for a fine tabular look at no cost of extra code.
</syntaxhighlight>
{| class="wikitable" style="margin: auto;"
 
|+ comparison default <syntaxhighlight lang="pascal" inline>write</syntaxhighlight> width
is equivalent to
|-
 
! colspan="2" | source code fragment
<syntaxhighlight lang="delphi">
|-
write('false':5);
| colspan="2" | <syntaxhighlight lang="pascal">writeLn(1);
write('true':4);
writeLn(23456789);</syntaxhighlight>
|-
 
! colspan="2" | produces in
in Delphi, but
|-
 
! Standard Pascal ([[GNU Pascal|GPC]]) !! Delphi
<syntaxhighlight lang="pascal">
|-
write('false':TotalWidth);
| <syntaxhighlight lang="text"> 1
write('true':TotalWidth);
23456789</syntaxhighlight>
| <syntaxhighlight lang="text">1
 
23456789</syntaxhighlight>
in ISO 7185.
|}
 
9. Temporary files are not supported by Delphi using traditional Pascal I/O: Executing Reset() or Rewrite() results in an error, where under Standard Pascal it opens a temporary file that exists for the duration of the program before being automatically deleted.
 
10. Notwithstanding the fact Delphi still allows using much of the traditional Pascal RTL, Delphi-specific library functionality is generally implemented in ordinary Delphi code that uses, rather than adds to, compiler-provided intrinsics. This philosophy contrasts to Standard Pascal, whose RTL must be provided by the compiler. For example, in Standard Pascal, the 'file' type is an intrinsic type—sort of like a record, but another compiler-defined type alongside records. In contrast, the TFileStream class in Delphi is an ordinary Delphi class written in ordinary Delphi code. Similarly, the Read, ReadLn, Write and WriteLn standard procedures have signatures that are not themselves legal Pascal signatures. While Delphi still implements them, only very rarely do Delphi-specific RTL routines require similarly special handling: rather, the language was evolved to include features such as 'open arrays', [[default parameter]]s and procedural overloading that can be used by user-written routines too.
 
== See also ==
 
* [[Delphi (programming language)]]
* [[Comparison of Pascal and C]]