[racket] returning self as sync result with struct prop:evt and wrap-evt

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Wed Jun 27 17:41:03 EDT 2012

At Wed, 27 Jun 2012 17:34:27 -0400, Neil Van Dyke wrote:
> If I have a struct that wraps an input-port, and I want the struct to be 
> usable as an event that returns the struct as the sync result... is 
> there an better way than the code below?

You can use a procedure that receives "self" as the `prop:evt' value.

------------------------------

#lang racket/base

(struct my-struct (my-wrapped-evt)
   #:property prop:evt (lambda (self)
                         (wrap-evt (my-struct-my-wrapped-evt self)
                                   (lambda (ignored)
                                     self))))

(define (my-struct-read s)
   (read-byte (my-struct-my-wrapped-evt s)))

;; Test:

(define s (my-struct (current-input-port)))

(printf "s = ~S\n" s)

(let loop ()
   (write-string "Syncing...\n")
   (let ((my-evt-result (sync s)))
     (printf "Reading from ~S...\n" my-evt-result)
     (let ((v (my-struct-read s)))
       (printf "Read ~S\n" v)
       (loop))))


Posted on the users mailing list.