[racket] Creating ids from symbols

From: Mark Carter (mcturra2000 at yahoo.co.uk)
Date: Sun Aug 7 05:35:27 EDT 2011

> From: Matthew Flatt <mflatt at cs.utah.edu>

>   http://docs.racket-lang.org/guide/reflection.html
> for more information about namespaces.

In the end, I couldn't get no loving from it. BUT, I did discover the following ...

Suppose I define the file cache.rkt:

(module cache racket
  (provide encache inspect-cache set-cache)
;(require racket/serialize)
(define cache (void))
(define-syntax encache
  (syntax-rules ()
    ((encache var)
     (set-cache (cons (quote var) (lambda () var))))))
  (define (set-cache v)
    (set! cache v))

(define (inspect-cache)
  (displayln (format "~a ~a" (car cache) ((cdr cache)))))


;; end

and a calling file foo.rkt:
#lang racket

(require (file "./cache.rkt"))

(define f 7)
(encache f)
(set! f 13)
(inspect-cache) ;=> f 13
;; end

It's far short of a full implementation, in that it only stores one value rather than many, doesn't serialize date to a file, and so on. But it does allow the module to take a sneak at the current value of the variable.

Maybe an interesting idea would be to create a way of creating a variable that represents the contents of some box, and see how that could be incorporated into a caching module.

Posted on the users mailing list.