• No results found

Language Keywords

In document Object Pascal Handbook (Page 35-39)

Keywords are all the identifiers reserved by the language. These are symbols that have a predefined meaning and role and you cannot use them in a different context.

Formally there is a distinction between reserved words and directives: Reserved words cannot be used as identifiers, while directives have a special meaning but could be used also in a different context (although you are recommended not to do so). In practice, you should not use any keyword as an identifier.

If you write some code like the following (where property is indeed a keyword):

var

property: string

you'll see an error message like:

E2029 Identifier expected but 'PROPERTY' found

In general when you misuse a keyword, you'll get different error messages depend-ing on the situation, as the compiler recognizes the keyword, but gets confused by its position in the code or by the following elements.

Here I don't want to show you a complete list of keywords, as some of them are rather obscure and rarely used, but only list a few, grouping them by their role. It will take me several chapters to explore all of these keywords and others I'm skip-ping in this list.

note Notice that some keywords can be used in different contexts, and here I'm generally referring only to the most common context (although a couple of keywords are listed twice). One of the reasons is that over the years the compiler team wanted to avoid introducing new keywords, as this might break existing applications, so they recycled some of the existing ones.

So let's start our exploration of keywords with some you've already seen in the initial demo source code and that are used to define the structure of an application project:

program Indicates the name of an application project library Indicates the name of a library project

package Indicates the name of a package library project unit Indicates the name of a unit, a source code file uses Refers to other units the code relies upon interface The part of a unit with declarations implementation The part of a unit with the actual code Marco Cantù, Object Pascal Handbook

initialization Code executed when a program starts finalization Code executed on program termination begin The start of a block of code

end The end of a block of code

Another set of keywords relates to the declaration of different basic data types and variables of such data types:

type Introduces a block of type declarations

var Introduces a block of variable declarations

const Introduces a block of constant declarations

set Defines a power set data type

string Defines a string variable or custom string type array Defines an array type

record Defines a record type integer Defines an integer variable real, single,

double Define floating point variables

file Defines a file

note There are many other data types defined in Object Pascal that I will cover later.

A third group includes keywords is used for the basic language statements, such a condition and loops, including also functions and procedures:

if Introduces a conditional statement

then Separates the condition from the code to execute

else Indicates possible alternative code

case Introduces a conditional statement with multiple options

of Separates the condition from the options

for Introduces a fixes repetitive cycle

to Indicates the final upper value of the for cycle downto Indicates the final lower value of the for cycle

Marco Cantù, Object Pascal Handbook

in Indicates the collection to iterate over in a cycle while Introduces a conditional repetitive cycle

do Separates the cycle condition from the code

repeat Introduces a repetitive cycle with a final condition until Indicates the final condition of the cycle

with Indicates a data structure to work with

function A sub-routine or group of statements returning a result

procedure A sub-routine or group of statements which doesn't return a result

inline Requests the compiler to optimize a function or procedure

overload Allows the reuse of the name of a function or pro-cedure

Many other keywords relate with classes and objects:

class Indicates a class type

object Used to indicate an older class type (now depre-cated)

abstract A class that is not fully defined

sealed A class from which other classes cannot inherit interface Indicates an interface type (listed also in the first

group)

constructor An object or class initialization method destructor An object or class cleanup method

virtual A virtual method

override The modified version of a virtual method inherited Refers to a method of the base class

private Portion of a class not accessible from the outside protected Portion of a class with limited access from the

outside

public Portion of a class fully accessible from the outside Marco Cantù, Object Pascal Handbook

published Portion of a class made specifically available to users

strict A stronger limitation for private and protected sections

property A symbol mapped to a value or method read The mapper for getting the value of a property write The mapper for setting the value of a property nil The value of a zero object (used also for other

entities)

A smaller group of keywords is used for exceptions handling (see Chapter 11):

try The start of an exception handling block

finally Introduces code to be executed regardless of an exception

except Introduces code to be executed in case of an exception

raise Used to trigger an exception

Another group of keywords is used for operators and is covered in the section

“Expressions and Operators” later in this chapter, (beside some advanced operators covered only in later chapters):

as and div

is in mod

not or shl

shr xor

Finally, here is partial list of other rarely used keywords, including some old ones you should really avoid using. Look them up in the help or in the index of this book, if you are interested in more information about these:

default dynamic export

exports external file

forward goto index

label message name

nodefault on out

packed reintroduce requires

Notice that the list of Object Pascal language keywords has seen very few additions over recent years, as any additional keyword implies potentially introducing

compi-Marco Cantù, Object Pascal Handbook

lation errors into some existing programs preventing that had happened to use one of the new keyword as a symbol. Most of the recent additions to the language required no new keyword, like generics and anonymous methods.

In document Object Pascal Handbook (Page 35-39)

Related documents