Type detection: Difference between revisions
Content added Content deleted
Line 92: | Line 92: | ||
print_text (source_t_string "This\nis a\ntext.\n"); |
print_text (source_t_string "This\nis a\ntext.\n"); |
||
print_text (source_t_FILEref f) |
print_text (source_t_FILEref f) |
||
end</pre> |
|||
===Using a linear '''dataviewtype'''=== |
|||
<lang ats>#include "share/atspre_staload.hats" |
|||
dataviewtype source_vt = |
|||
| source_vt_string of string |
|||
| source_vt_FILEref of FILEref |
|||
extern fun |
|||
print_text (source : source_vt) : void |
|||
implement |
|||
print_text (source) = |
|||
case+ source of |
|||
| ~ source_vt_string s => print! (s) |
|||
| ~ source_vt_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_opt = fileref_open_opt ("type_detection-postiats-2.dats", |
|||
file_mode_r) |
|||
in |
|||
print_text (source_vt_string "This\nis a\ntext.\n"); |
|||
case+ f_opt of |
|||
| ~ Some_vt f => print_text (source_vt_FILEref f) |
|||
| ~ None_vt () => fprintln! (stderr_ref, "Failed to open the file.") |
|||
end</lang> |
|||
{{out}} |
|||
$ patscc -DATS_MEMALLOC_LIBC type_detection-postiats-2.dats |
|||
<pre>This |
|||
is a |
|||
text. |
|||
#include "share/atspre_staload.hats" |
|||
dataviewtype source_vt = |
|||
| source_vt_string of string |
|||
| source_vt_FILEref of FILEref |
|||
extern fun |
|||
print_text (source : source_vt) : void |
|||
implement |
|||
print_text (source) = |
|||
case+ source of |
|||
| ~ source_vt_string s => print! (s) |
|||
| ~ source_vt_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_opt = fileref_open_opt ("type_detection-postiats-2.dats", |
|||
file_mode_r) |
|||
in |
|||
print_text (source_vt_string "This\nis a\ntext.\n"); |
|||
case+ f_opt of |
|||
| ~ Some_vt f => print_text (source_vt_FILEref f) |
|||
| ~ None_vt () => fprintln! (stderr_ref, "Failed to open the file.") |
|||
end</pre> |
end</pre> |
||