Type detection: Difference between revisions

Content added Content deleted
Line 18: Line 18:
If not practical, show how the caller would coerce a type that can be passed to the library function.
If not practical, show how the caller would coerce a type that can be passed to the library function.
<br><br>
<br><br>

=={{header|ATS}}==
===Using garbage collection and '''datatype'''===
<lang ats>#include "share/atspre_staload.hats"

datatype source_t =
| source_t_string of string
| source_t_FILEref of FILEref

extern fun
print_text (source : source_t) : void

implement
print_text (source) =
case+ source of
| source_t_string s => print! (s)
| source_t_FILEref f =>
let
var c : int = fileref_getc (f)
in
while (0 <= c)
begin
fileref_putc (stdout_ref, c);
c := fileref_getc (f)
end
end

implement
main0 () =
let
val f = fileref_open_exn ("type_detection-postiats.dats",
file_mode_r)
in
print_text (source_t_string "This\nis a\ntext.\n");
print_text (source_t_FILEref f)
end</lang>

{{out}}
$ patscc -DATS_MEMALLOC_GCBDW type_detection-postiats.dats -lgc && ./a.out
<pre>This
is a
text.
#include "share/atspre_staload.hats"

datatype source_t =
| source_t_string of string
| source_t_FILEref of FILEref

extern fun
print_text (source : source_t) : void

implement
print_text (source) =
case+ source of
| source_t_string s => print! (s)
| source_t_FILEref f =>
let
var c : int = fileref_getc (f)
in
while (0 <= c)
begin
fileref_putc (stdout_ref, c);
c := fileref_getc (f)
end
end

implement
main0 () =
let
val f = fileref_open_exn ("type_detection-postiats.dats",
file_mode_r)
in
print_text (source_t_string "This\nis a\ntext.\n");
print_text (source_t_FILEref f)
end</pre>


=={{header|AWK}}==
=={{header|AWK}}==