show
int show(var self);
int show_to(var self, var out, int pos);
Show the object self
either to stdout
or to the object output
.
look
int look(var self);
int look_from(var self, var input, int pos);
Read the object self
either from stdout
or from the object input
.
#define print(fmt, ...)
#define println(fmt, ...)
#define print_to(out, pos, fmt, ...)
int print_with(const char* fmt, var args);
int println_with(const char* fmt, var args);
int print_to_with(var out, int pos, const char* fmt, var args);
Print the format string fmt
either to stdout
or to the object out
at positions pos
. Returns new position in output.
scan
#define scan(fmt, ...)
#define scanln(fmt, ...)
#define scan_from(input, pos, fmt, ...)
int scan_with(const char* fmt, var args);
int scanln_with(const char* fmt, var args);
int scan_from_with(var input, int pos, const char* fmt, var args);
Scan the format string fmt
either from stdout
or from the object input
at position pos
. Returns new position in output.
Hello World
println("Hello %s!", $S("World"));
File Writing
with (f in new(File, $S("prices.txt"), $S("wb"))) {
print_to(f, 0, "%$ :: %$\n", $S("Banana"), $I(57));
print_to(f, 0, "%$ :: %$\n", $S("Apple"), $I(22));
print_to(f, 0, "%$ :: %$\n", $S("Pear"), $I(16));
}
String Scanning
var input = $S("1 and 52 then 78");
var i0 = $I(0), i1 = $I(0), i2 = $I(0);
scan_from(input, 0, "%i and %i then %i", i0, i1, i2);
/* i0: 1, i1: 52, i2: 78 */
println("i0: %$, i1: %$, i2: %$", i0, i1, i2);
String Printing
var greeting = new(String);
print_to(greeting, 0, "Hello %s %s, %s?",
$S("Mr"), $S("Johnson"), $S("how are you?"));
/* Hello Mr Johnson, how are you? */
show(greeting);
Convert To or From String
The Show
class is used to convert objects to, and from, a String
representation. Objects which implement Show
should expect the input/output object to be one that support the Format
class, such as File
or String
.
The print
, println
and print_to
functions provide a mechanism for writing formatted strings with Cello objects. To do this they provide a new format specifier %$
which uses an object's Show
functionality to write that part of the string. All objects which don't support Show
can still be shown via a default implementation.
All the Show methods which are variable arguments only take var
objects as input. To print native C types wrap them in Cello types using $
.
Standard format specifiers such as %f
and %d
will call functions such as c_float
and c_int
on their passed arguments to convert objects to C types before performing the standard C formatting behaviour.
See printf for more information on format specifiers.
struct Show {
int (*show)(var, var, int);
int (*look)(var, var, int);
};