Communication ports categories
I/O, Modules and System Interface
Printing
WCL syntax

Printing

There are several ways of printing in CLAIRE. Any entity may be printed with the function print. When print is called for an object that does not inherit from thing (an object without a name), it calls the method self_print of which you can define new restrictions whenever you define new classes. If self_print was called on an object x owned by a class toto for which no applicable restriction could be found, it would print <toto>.

In the case of bags (sets or lists), strings, symbols or characters, the standard method is princ. It formats its argument in a somewhat nicer way than print. For example :
 print("john"prints "john"
 princ("john"prints john
Finally, there also exists a printf macro as in C. Its first argument is a string with possible occurrences of the control patterns ~S, ~I and ~A. The macro requires as many arguments as there are "tilde patterns" in the string, and pairs in order of appearance arguments together with tildes. These control patterns do not refer to the type of the corresponding argument but to the way you want it to be printed. The macro will call print for each argument associated with a ~S form, princ for each associated with a ~A form and will print the result of the evaluation of the argument for each ~I form. A mnemonic is A for alphanumeric, S for standard and I for instruction. Hence the command :
 printf("~S is ~A and here is what we know\n ~I"john23show(john))
will be expanded into :
 (print(john), princ(" is "), princ(23),
     princ(" and here is what we know\n"),
         show(john))
Output may also be directed to a file or another device instead of the screen, using a port. A port is an object bound to a physical device, a memory buffer or a file. The method use_as_output is meant to select the port on which the output will be written. Following is an example :
 let p := fopen("agenda-2006""w")
 in (use_as_output(p),
     write(agenda), fclose(p))
[XL] In XL CLAIRE printf construction can take a port argument and would perform a local output rediction to the supplied port :
 printf(my_port"~S is ~A and here is what we know\n ~I"john23show(john))
will be expanded into :
 let old := use_as_output(my_port)
 in (print(john), princ(" is "), princ(23),
     princ(" and here is what we know\n"), show(john),
  use_as_output(old))
CLAIRE also offers a simple method to redirect the output towards a string port. Two methods are needed to do this: print_in_string and end_of_string. print_in_string() starts redirecting all printing statements towards the string being built. end_of_string() returns the string formed by all the printing done between these two instructions. You can only use print_in_string with one output string at a time; more complex uses require the creation of multiple string ports.

All trace statements will be directed to this port. A trace statement is either obtained implicitly through tracing a method or a rule, or explicitly with the trace statement. the statement trace(n, <string>, <args> ...) is equivalent to printf(<string>, <args> ..) with two differences: the string is printed only if the verbosity level verbose() is higher than n and the output port is ctrace(). The following lines are equivalent :
 trace(0"assigning ~S with ~S"xy)
 //[0] assigning ~S with ~S // xy
 (if (verbose() >= 0printf(ctrace(), "assigning ~S with ~S"xy))
[XL] In XL CLAIRE however, trace instructions are bound to a module such one can specify a per module verbose policy : a module m has a slot m.verbose that can take the values :


categories Printingnormal dispatch Core method

end_of_string() -> string

end_of_string() returns the string containing everything that has been printed since the last call to print_in_string().


categories Printingnormal dispatch Core method

format(self:string, larg:list) -> void

This method does the same thing as printf, except that there are always two arguments, thus the arguments must be replaced by an explicit list. Unlike with the printf construction ~I cannot be used in a format. Last the string s may contain color escapes.


categories Printingnormal dispatch Kernel method

princ(n:integer) -> void

equivalent to print(n)


categories Printingnormal dispatch Kernel method

princ(s:string) -> void

princ(s) prints the string s on the current output (cout()).


categories Printingnormal dispatch Kernel method

princ(c:char) -> void

princ(c) prints the char c on the current output (cout()).


categories Printingnormal dispatch Kernel method

princ(s:bag) -> void

prints the content of the bag l on the current output (cout()). elements are separated by a comma ','.


categories Printingnormal dispatch [XL] Kernel method

princ(s:string, i:integer, j:integer) -> void

princ(s,i,j) is equivalent to princ(substring(s,i,j)) but without the allocation of a temporary string


categories Printingnormal dispatch Kernel method

print(x:any) -> void

prints the entity x (x can be anything) on the current output (cout()).


categories Printingnormal dispatch Core method

print_in_string() -> void

print_in_string() opens a new output port that will be stored as a string. The user is given access to the string at the end of the transcription, when the call to end_of_string() returns this string. print_in_string may be used recursively.


categories Printingnormal dispatch Kernel interface

self_print :: property(open = 3)

self_print interface is used to redefine the way an entity is printed by the standard print method. self_print can be redefined for each new created class, for instance :
 account <: ephemeral_object(account_id:integer)

 self_print(self:account: void ->
     printf("<account with id ~S>"self.account_id)