Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 61 lines (45 sloc) 1.567 kb
dad53043 »
2010-08-14 updated readme
1 The Clojure debug-repl
2
3 Every time I stick a println into some Clojure code to debug it, I
4 think to myself, "This is Lisp! I should be able to insert a repl
5 here!"
6
7 The problem is of course that Clojure's eval function doesn't know
8 about the surrounding lexical scope. How to solve the problem?
9 Create a macro that passes a copy of the lexical scope in with the
10 form to be evaled, something like this:
11
12 (defn eval-with-locals
13 [locals form]
14 (eval
15 `(let ~(generate-local-bindings locals)
16 ~form)))
17
18 USE:
19 The interface is meant to be dead simple:
20 "(use 'alex-and-georges.debug-repl)" loads it, and "(debug-repl)" invokes it.
21
22 That's about it. When you enter the debug-repl, the regular
23 repl prompt will be replaced with
24
25 dr =>
26
27 An example will make it clearer:
28
29 user=> (let [c 1 d 2]
30 (defn a [b c]
31 (debug-repl)
32 d))
33 #'user/a
34
35 user=> (a "foo" "bar")
36
37 dr-1-1001 => c
38 "bar"
39
40 dr-1-1001 => d
41 2
42
43 dr-1-1001 => *locals*
44 {fn__104 #<user$eval__103 user$eval__103@5f6303>, c "bar", d 2, fn__106 #<user$eval__103$a__105 user$eval__103$a__105@179dce4>, b "foo", counter__56__auto__ 1001}
45
46 dr-1-1001 => (str b c)
47 "foobar"
48
49 dr-1-1001 => ()
50 2
51 user=>
52
53 LIMITATIONS:
54 The debug-repl doesn't currently integrate properly
55 with the slime-repl, (I think because of how Slime manages IO
56 redirection,) so you'll have to invoke it from a regular repl, or
57 slime's *inferior lisp* buffer.
58
59 A version of the debug-repl has been ported to slime. More details
60 here:
61 http://hugoduncan.org/post/2010/swank_clojure_gets_a_break_with_the_local_environment.xhtml
62
Something went wrong with that request. Please try again.