This is a revised proposal for adding custom I/O ports to R7RS small Scheme. I am publishing this document to invite wide comment. There is nothing official about it. I retain sole responsibility for it, including all errors. This replaces my original part 7 completely.
Custom ports in R6RS are like those in most systems: basically a set of procedures which are invoked when standard port operations are executed. For example, if you create a custom character input port, you may then call read on it, and the normal read algorithm will be invoked, but it will read characters not from a file or a string, but rather from one of the procedures set up when the port was created, the one corresponding to read-char. However, the R6RS interface to creating custom ports, like all others I have seen, is highly inflexible. The set of procedures varies depending on whether the port is input or output, binary or character-based. If you get the order of procedures wrong, you lose. And if new basic procedures or port types are added by an implementation, the existing port constructors will require supplementation with new constructors. Therefore, I am proposing a pattern whereby a custom port is not a record of procedures, but a map from symbols to procedures. When such a port is created, it has a variety of default behaviors corresponding to the standard port operations. The resulting port isn't good for anything much. To make the port useful, you replace its behaviors: for example, you can redefine the behavior of read-char in a custom input port with an implementation-dependent procedure that reads from a network port. The difference between custom input and output ports, however, is hard-coded into the port when it is created. 5 identifiers are bound to procedures by this proposal: (make-custom-input-port) returns a custom input port with default behaviors. (make-custom-output-port) returns a custom output port with default behaviors. (custom-port-get custom-port symbol) returns the behavior of the custom-port associated with the symbol. The standard symbols are the names of standard procedures, and the procedures returned take the same number of arguments with the same semantics as those standard procedures. (custom-port-set! custom-port symbol procedure) changes the behavior associated with symbol in custom-port to procedure. (custom-port-remove! custom-port symbol) reverts the behavior associated with symbol in custom-port to the default. Here are the 6 standard symbols for custom input ports and their default behaviors: read-char Returns an eof-object peek-char Returns an eof-object read-u8 Returns an eof-object peek-u8 Returns an eof-object port-u8-position Does nothing set-port-u8-position! Does nothing Here are the 4 standard symbols for custom output ports and their default behaviors. write-char Does nothing write-u8 Does nothing port-u8-position Does nothing set-port-u8-position! Does nothing -- Babies are born as a result of the John Cowan mating between men and women, and most http://www.ccil.org/~cowan men and women enjoy mating. [email protected] --Isaac Asimov in Earth: Our Crowded Spaceship _______________________________________________ r6rs-discuss mailing list [email protected] http://lists.r6rs.org/cgi-bin/mailman/listinfo/r6rs-discuss
