vvvvalvalval / scope-capture

Project your Clojure(Script) REPL into the same context as your code when it ran
MIT License
573 stars 14 forks source link

Cool discovery: you can nest `letsc`! #33

Closed DjebbZ closed 4 years ago

DjebbZ commented 5 years ago

Hi,

I've just discovered that when one captures with spy at different places of the code, even if they're executed at different moments (imagine 2 different HTTP requests to the same server), you can nest the letsc to access both contexts at the same time! Ex:

(letsc 1 ;; id of the first capture, for the first HTTP request
  (letsc 2 ;; if of the 2nd
    [res1 res2])) ;; I can access both HTTP responses (provided the names of the vars don't clash I suppose)

It works like nesting normal lets. I have no idea if it was obvious to you, but for me this discovery makes scope-capture even better-er. Should it be documented somehow? Only stating the possibility is enough, like "You can nest letsc like you nest let".

vvvvalvalval commented 5 years ago

Hi @DjebbZ, nice catch, thanks for the suggestion. I wouldn't put this in the docstring, but this looks like a good first entry for a 'Tips and Tricks' documentation chapter - would you like contributing it in the doc/ folder and linking it from the README?

I could imagine something like the following:

Combining locals from several saved contexts

Problem: sometimes, you would like to evaluate an expression which uses locals saved in different Execution Points. For instance, you may need a req local from an HTTP request handler, and a db local from a data-fetching function.

One way to achieve that effect is to nest to sc.api/letsc calls:

(sc.api/letsc [3 -1] ;; will bring `req` into scope
  (sc.api/letsc [5 -2] ;; will bring `db` into scope
    (do-something-with req db)))

Caveat: Of course, locals from the inner letsc may shadow locals from the outer.

TODO: same tip with defsc.

vvvvalvalval commented 4 years ago

Added to the Tips and Tricks doc page.