Skip to content

Instantly share code, notes, and snippets.

@cespare
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cespare/6f966059a1b49f3f437c to your computer and use it in GitHub Desktop.
Save cespare/6f966059a1b49f3f437c to your computer and use it in GitHub Desktop.
; I've got a function like this:
(defn parse-from-bytes [type bytes] ...)
; that knows how to parse a binary protocol into a certain type.
; Custom parsers are generated for each type by a macro, say gen-parser:
(defmacro gen-parser [type] ...)
; So when you call parse-from-bytes, it checks to see whether a parser
; was previously generated for that type, and calls it.
;
; My thought for how to accomplish this was to define a private atom
(def ^{:private true} parser-cache (atom {}))
; and then have gen-parser emit code like
(swap! #'ns/parser-cache assoc type parse-fn)
; But this doesn't work because swap sees a var and not an atom.
; So my questions:
; 1. Is there a better overall approach I should be using?
; 2. (Assuming I want to keep parser-cache private) how can I refer
; to a private atom?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment