Type detection: Difference between revisions
Content added Content deleted
Line 1,071: | Line 1,071: | ||
} |
} |
||
end</pre> |
end</pre> |
||
=={{header|Pascal}}== |
|||
===Dispatch by variant record=== |
|||
Pascal has a plethora of dialects, and so I have tried to be as close to ''Algorithms + Data Structures = Programs'' (Wirth) style as I could figure out how, when using the Free Pascal Compiler. |
|||
<lang pascal>program type_detection_demo (input, output); |
|||
type |
|||
source_t = record case kind : (builtintext, filetext) of |
|||
builtintext : (i : integer); |
|||
filetext : (f : file of char); |
|||
end; |
|||
var |
|||
source : source_t; |
|||
input : file of char; |
|||
c : char; |
|||
procedure print_text (var source : source_t); |
|||
begin |
|||
case source.kind of |
|||
builtintext : case source.i of |
|||
1 : writeln ('This is text 1.'); |
|||
2 : writeln ('This is text 2.') |
|||
end; |
|||
filetext : while not eof (source.f) do |
|||
begin |
|||
read (source.f, c); |
|||
write (c) |
|||
end |
|||
end |
|||
end; |
|||
begin |
|||
assign (input, 'type_detection-pascal.pas'); |
|||
reset (input); |
|||
with source do |
|||
begin |
|||
kind := builtintext; |
|||
i := 1; |
|||
print_text (source); |
|||
i := 2; |
|||
print_text (source); |
|||
kind := filetext; |
|||
f := input; |
|||
print_text (source) |
|||
end |
|||
end.</lang> |
|||
{{out}} |
|||
$ fpc -Miso type_detection-pascal.pas && ./type_detection-pascal |
|||
<pre>This is text 1. |
|||
This is text 2. |
|||
program type_detection_demo (input, output); |
|||
type |
|||
source_t = record case kind : (builtintext, filetext) of |
|||
builtintext : (i : integer); |
|||
filetext : (f : file of char); |
|||
end; |
|||
var |
|||
source : source_t; |
|||
input : file of char; |
|||
c : char; |
|||
procedure print_text (var source : source_t); |
|||
begin |
|||
case source.kind of |
|||
builtintext : case source.i of |
|||
1 : writeln ('This is text 1.'); |
|||
2 : writeln ('This is text 2.') |
|||
end; |
|||
filetext : while not eof (source.f) do |
|||
begin |
|||
read (source.f, c); |
|||
write (c) |
|||
end |
|||
end |
|||
end; |
|||
begin |
|||
assign (input, 'type_detection-pascal.pas'); |
|||
reset (input); |
|||
with source do |
|||
begin |
|||
kind := builtintext; |
|||
i := 1; |
|||
print_text (source); |
|||
i := 2; |
|||
print_text (source); |
|||
kind := filetext; |
|||
f := input; |
|||
print_text (source) |
|||
end |
|||
end.</pre> |
|||
=={{header|Perl}}== |
=={{header|Perl}}== |