본문 바로가기
델파이/책-문서-동영상

Modern Object Pascal Introduction forProgrammers

by rokwha 2025. 4. 16.

https://castle-engine.io/modern_pascal_introduction.pdf
https://cafe.naver.com/delmadang/3217

 

Michalis Kamburelis

 

Table of Contents

더보기

Table of Contents

 

1. Why ....................................................................................................................... 3

 

2. Basics .................................................................................................................... 4

2.1. "Hello world" program ................................................................................. 4

2.2. Functions, procedures, primitive types ....................................................... 4

2.3. Testing (if) ................................................................................................... 7

2.4. Logical, relational and bit-wise operators ................................................... 8

2.5. Testing single expression for multiple values (case) ................................... 9

2.6. Enumerated and ordinal types and sets and constant-length arrays ......... 10

2.7. Loops (for, while, repeat, for .. in) ............................................................ 11

2.8. Output, logging ......................................................................................... 14

2.9. Converting to a string ............................................................................... 15

 

3. Units .................................................................................................................... 16

3.1. Overview ................................................................................................... 16

3.2. Extensions used for units and programs .................................................. 17

3.3. Initialization and finalization ...................................................................... 18

3.4. Units using each other .............................................................................. 19

3.5. Qualifying identifiers with unit name ......................................................... 20

3.6. Exposing one unit identifiers from another ............................................... 23

 

4. Classes ............................................................................................................... 24

4.1. Basics ....................................................................................................... 24

4.2. Inheritance, is, as ..................................................................................... 24

4.3. Properties .................................................................................................. 27

4.4. Exceptions - Quick Example ..................................................................... 30

4.5. Visibility specifiers ..................................................................................... 31

4.6. Default ancestor ........................................................................................ 32

4.7. Self ............................................................................................................ 32

4.8. Calling inherited method ........................................................................... 32

4.9. Virtual methods, override and reintroduce ................................................ 35

 

5. Freeing classes ................................................................................................... 39

5.1. Remember to free the class instances ..................................................... 39

5.2. How to free ............................................................................................... 39

5.3. Manual and automatic freeing .................................................................. 41

5.4. The virtual destructor called Destroy ........................................................ 44

5.5. Free notification ........................................................................................ 45

5.6. Free notification observer (Castle Game Engine) ..................................... 47

 

6. Exceptions ........................................................................................................... 49

6.1. Overview ................................................................................................... 49

6.2. Raising ...................................................................................................... 50

6.3. Catching .................................................................................................... 51

6.4. Finally (doing things regardless if an exception occurred) ........................ 54

6.5. How the exceptions are displayed by various libraries ............................. 57

 

7. Run-time library ................................................................................................... 58

7.1. Input/output using streams ....................................................................... 58

7.2. Containers (lists, dictionaries) using generics ........................................... 59

7.3. Cloning: TPersistent.Assign ...................................................................... 65

 

8. Various language features .................................................................................. 70

8.1. Local (nested) routines ............................................................................. 70

8.2. Callbacks (aka events, aka pointers to functions, aka procedural variables) .......................................................................................................... 71

8.3. Anonymous functions ................................................................................ 74

8.4. Generics .................................................................................................... 77

8.5. Overloading ............................................................................................... 79

8.6. Preprocessor ............................................................................................. 79

8.7. Records ..................................................................................................... 82

8.8. Variant records and related concepts ....................................................... 83

8.9. Old-style objects ....................................................................................... 87

8.10. Pointers ................................................................................................... 87

8.11. Operator overloading .............................................................................. 88

 

9. Advanced classes features ................................................................................. 91

9.1. Private and strict private ........................................................................... 91

9.2. More stuff inside classes and nested classes ........................................... 92

9.3. Class methods .......................................................................................... 93

9.4. Class references ....................................................................................... 94

9.5. Static class methods ................................................................................. 96

9.6. Class properties and variables ................................................................. 98

9.7. Class helpers ............................................................................................ 99

9.8. Virtual constructors, destructors ............................................................. 100

9.9. An exception in constructor .................................................................... 101

 

10. Interfaces ........................................................................................................ 102

10.1. Interfaces GUIDs .................................................................................. 104

10.2. Typecasting interfaces .......................................................................... 105

10.3. CORBA and COM types of interfaces .................................................. 108

10.4. Reference-counted (COM) interfaces ................................................... 110

10.5. Using COM interfaces with reference-counting disabled ....................... 113

11. About this document ....................................................................................... 115

There are many books and resources about Pascal out there, but too many of them
talk about the old Pascal, without classes, units or generics.

 

So I wrote this quick introduction to what I call modern Object Pascal. Most of the
programmers using it don’t really call it "modern Object Pascal", we just call it "our
Pascal".

 

But when introducing the language, I feel it’s important to emphasize that it’s
a modern, object-oriented language. It evolved a lot since the old (Turbo) Pascal that
many people learned in schools long time ago. Feature-wise, it’s quite similar to C+ or Java or C#.

 

• It has all the modern features you expect — classes, units, interfaces, generics…
• It’s compiled to a fast, native code,
• It’s very type safe,
• High-level but can also be low-level if you need it to be.

 

It also has excellent, portable and open-source compiler called the Free Pascal Compiler, http://freepascal.org/And an accompanying IDE (editor, debugger, a library of visual components, form designer) called Lazarus http://lazarus.freepascal.org/There’s also a proprietary and commercial compiler and IDE Delphi https://www.embarcadero.com/products/Delphi 

 

There’s a lot of libraries (for both FPC and Delphi) available, see https://github.com/Fr0sT-Brutal/awesome-pascal . We also support existing editors like VS Code, see https://castle-engine.io/vscode .

 

Myself, I’m the creator of Castle Game Engine, https://castle-engine.io/ , which is an open-source 3D and 2D game engine using modern Pascal to create games on many platforms (Windows, Linux, macOS, Android, iOS, Nintendo Switch; also WebGL is coming).


This introduction is mostly directed at programmers who already have experience in other languages. We will not cover here the meanings of some universal concepts, like "what is a class", we’ll only show how to do them in Pascal.


modern_pascal_introduction.pdf
0.48MB

 

반응형