On 2020-03-25 14:16, David Storrs wrote:
This would let me use a struct as an output port:
(struct foo (name out) #:property prop:output-port (struct-field-index out))
I'd like to be able to set up a struct such that I can use it as a UDP
socket, something like the following pseudo-code:
(struct foo (socket) #:property prop:udp (struct-field-index socket))
I see that Racket defines prop:input-port and prop:output-port, but
there's no prop:udp, nor can I figure out how to do it through the
structure type properties or generic interfaces. (I feel like I'm
missing something on those two, so maybe it's just poor understanding.)
Is there a way to do this?
The way those properties work is that functions that use input-ports and
outputs-ports have knowledge about the properties and check if it is a
struct with those properties. There is no prop:udp though. You could
make one (like the following), but you would need to make your own
wrappers of the existing udp-* functions and have them do the coercion
before passing arguments to the existing racket functions.
Cheers,
Sam
--- >8 --- >8 ---
#lang racket/base
(require (prefix-in - racket/udp))
(define-values (prop:udp prop:udp? udp-socket-ref)
(make-struct-type-property
'prop:udp
(lambda (val struct-info)
(cond
[(procedure? val) val]
[(exact-nonnegative-integer? val)
; XXX: check for valid index
(let ([struct-ref (list-ref struct-info 3)])
(lambda (a-struct-value)
(struct-ref a-struct-value val)))]))))
(define (->udp-socket v)
(cond
[(-udp? v) v]
[else
(->udp-socket ((udp-socket-ref v) v))]))
;; Something like this for every udp function
(define (udp-bind! socket hostname-string port-no [reuse? #f])
(-udp-bind! (->udp-socket socket)
hostname-string
port-no
reuse?))
(struct server (socket)
#:property
prop:udp (struct-field-index socket)
#:transparent)
(define (make-server)
(let ([udp (-udp-open-socket)])
(server udp)))
--
You received this message because you are subscribed to the Google Groups "Racket
Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit
https://groups.google.com/d/msgid/racket-users/34622180-8073-1e61-8187-f7d9dc48b236%40gmail.com.