Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does WriteLn() really work?

Since the age of the dinosaurs, Turbo Pascal and nowadays Delphi have a Write() and WriteLn() procedure that quietly do some neat stuff.

  • The number of parameters is variable;

  • Each variable can be of all sorts of types; you can supply integers, doubles, strings, booleans, and mix them all up in any order;

  • You can provide additional parameters for each argument:

Write('Hello':10,'World!':7); // alignment parameters

  • It even shows up in a special way in the code-completion drowdown:
    • Write ([var F:File]; P1; [...,PN] )
    • WriteLn ([var F:File]; [ P1; [...,PN]] )

Now that I was typing this I've noticed that Write and WriteLn don't have the same brackets in the code completion dropdown. Therefore it looks like this was not automatically generated, but it was hard-coded by someone.

Anyway, am I able to write procedures like these myself, or is all of this some magic hardcoded compiler trickery?

like image 417
Wouter van Nifterick Avatar asked Mar 06 '09 04:03

Wouter van Nifterick


People also ask

What is the purpose of writeln?

Writeln is an extension of the Write procedure, as it is defined for text files. The syntax shown here for the Writeln procedure is illustrates that WriteLn can take a variable number of arguments. After executing Write, Writeln writes an end-of-line marker (line feed or carriage return/line feed) to the file.

What does writeln mean in programming?

Description. WriteLn does the same as Write for text files, and emits a Carriage Return - LineFeed character pair after that. If the parameter F is omitted, standard output is assumed. If no variables are specified, a newline character sequence is emitted, resulting in a new line in the file F .


2 Answers

Writeln is what we call a compiler "magic" function. If you look in System.pas, you won't find a Writeln that is declared anything like what you would expect. The compiler literally breaks it all down into individual calls to various special runtime library functions.

In short, there is no way to implement your own version that does all the same things as the built-in writeln without modifying the compiler.

like image 138
Allen Bauer Avatar answered Nov 02 '22 07:11

Allen Bauer


As the Allen said you can't write your own function that does all the same things.

You can, however, write a textfile driver that does something custom and when use standard Write(ln) to write to your textfile driver. We did that in ye old DOS days :)

("Driver" in the context of the previous statement is just a piece of Pascal code that is hooked into the system by switching a pointer in the System unit IIRC. Been a long time since I last used this trick.)

like image 36
gabr Avatar answered Nov 02 '22 06:11

gabr