[plt-scheme] Re: How to write context-aware macros?
Thank you both for your replies, I hadn't thought of this approach
before. I guess what I was really looking for was alternatives to my
usual pattern for programming against an interface. My typical
approach is to define the interface in a struct, create convenience
functions to access lambdas in the struct, then each implementation
puts the appropriate lambdas in the struct.
For example, assume a datastore-like interface with 'get' and 'put'
operations. My approach would be:
; "datastore.ss"
(define-struct ds (get put))
(define (ds/get ds key) ((ds-get ds) key))
(define (ds/put ds key value) ((ds-put ds) key value))
(provide make-datastore ds/get ds/put)
; "file-datastore-implementation.ss"
(require "datastore.ss")
(define make-file-datastore
(make-datastore
(lambda (key) <get the value based on the key...>)
(lambda (key value) <put the key/value...>)))
(provide make-file-datastore)
Thus datastore consumers only need "datastore.ss" and the
implementation can be set-up and provided by the consumer's
controller. I'm not sure how canonical this approach is, or whether it
is a legacy I've brought from exposure to object orientation, but it
works for me. It 'feels' lighter than using a full object system. But
I also like the expressiveness of the clojure example. Are there other
techniques that may be considered 'du jour' for programming against
encapsulated components, or am I still thinking too much in terms of
object orientation?