Create a gist now

Instantly share code, notes, and snippets.

#lang racket
;; Let's create a custom structure for procedures that remember certain things.
(struct proc/data (proc data)
#:property prop:procedure (lambda (pd . args)
(apply (proc/data-proc pd) args)))
;; We can manually create such instances...
(define f (proc/data
;; implementation:
(lambda (x)
(* x x))
;; with associated metadata:
"this is the square function"))
;; It can be called:
(f 16)
;; but we can also retrieve its metadata:
(proc/data-data f)
;; Now let's have a def form that remembers module information.
;; This "def" will try to automatically bundle a metadata or a proc/data; this information is
;; the module path, which it can determine during compilation.
(define-syntax (def stx)
(syntax-case stx ()
[(_ (name . args) body ...)
(syntax/loc stx
(define name (proc/data
;; The procedure implementation as before:
(lambda args body ...)
;; Plus some metadata whose content we can compute at compile-time:
(let-syntax ([get-literal-metadata
(lambda (stx)
#`(#%datum . #,(format "~s" (variable-reference->resolved-module-path
(provide def
(struct-out proc/data))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment