[racket] Example of using a struct as a port

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Sat Apr 21 12:35:01 EDT 2012

On Apr 21, 2012, at 11:52 AM, Chad Albers wrote:

> Hi,
> I'm looking for an example of treating a struct like a port, which appears to be possible: http://docs.racket-lang.org/reference/portstructs.html?q=struct#(def._((quote._~23~25kernel)._prop~3ainput-port))
> Apparently, this is a lot easier that using make-custom-port.   What I would like to do is be able to declare a struct, set it's prop:input-port property, pass an instance of struct to a port procedure, like (read-bytes string-that-acts-like-port), and have the bytes sent to the read-bytes actually contain bytes that were stored in the fields of the struct itself.  Is that possible, and is that the reason why one would treat a struct like a port?
> Thanks in advance for any examples,

Do you mean something like this: 

#lang racket
(struct i (x) #:property prop:input-port 0)
(define an-i (i (open-input-string "hello world")))
(read an-i) ;; yields 'hello 

-- Matthias

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20120421/3e58212f/attachment.html>

Posted on the users mailing list.