-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.pas
105 lines (86 loc) · 2.25 KB
/
main.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
{
CudaText: lexer_file=Pascal; tab_size=2; tab_spaces=Yes; newline=LF;
}
{$mode objfpc}
{$H+}
{$modeswitch anonymousfunctions}
{$modeswitch functionreferences}
program main;
uses
fpcdefer,
sysutils;
type
tSomeClass = class
strict private
name: string;
count: integer;
procedure message (const msg: string);
public
procedure hello;
constructor create (const _name: string);
destructor destroy; override;
end;
procedure tSomeClass.message (const msg: string);
begin
inc(count);
writeln(format('%d: %s %s', [count, name, msg]));
end;
procedure tSomeClass.hello;
begin
message('Greetings!');
end;
constructor tSomeClass.create (const _name: string);
begin
name := _name;
message('now open');
end;
destructor tSomeClass.destroy;
begin
message(format('now closed! (public methods called before destruction = %d)', [count]));
inherited;
end;
procedure goodbye;
begin
writeln('Goodbye!');
end;
procedure main;
var
defer: tDefer;
y: integer;
something: tSomeClass;
procedure deferred;
begin
inc(y);
writeln(format('Goodbye World! y = %d', [y]));
end;
begin
defer.x := @goodbye;
something := tSomeClass.create('procastination 101');
// Both of these do the same thing
// defer.add(some function reference)
// defer.x := some function reference
// should really use a smart pointer for freeing, this is an example only
defer.add(@something.free);
// this is fine, but may not work with smart pointers if
// already freed due to forced reference count decrement
// smart pointer finalize was called first and already freed it
defer.x := @something.hello;
something.hello;
y := 4;
defer.x := procedure
begin
writeln('First added runs last! y=', y);
end;
defer.x := procedure
begin
writeln('Last added runs first! y=', y);
end;
// type cast needed because deferred is a local function
defer.x := tDeferProc(@deferred);
writeln('Hello, World!');
writeln(format('y : %d', [y]));
defer.anchor;
end;
begin
main;
end.