Arc Forumnew | comments | leaders | submitlogin
1 point by evanrmurphy 4839 days ago | link | parent

To avoid polluting the global scope, you could keep your variables in a table instead:

  ; Our scope, which we'll use instead of the global scope

  (= dynamic-env* (table))
  
  ; Initial value 

  (= (dynamic-env* 'x) 5)
  
  ; Like your some-fn, just a function that uses x.
  ; Except that this references x in the table instead
  ; of the global scope 

  (def foo () (prn "x = " (dynamic-env* 'x)))
  
  ; Like let, but manipulates keys in our table
  ; instead of variables in the global scope 

  (mac dynamic-env*-let (var val . body)
    (w/uniq g-old-val
      `(let ,g-old-val (dynamic-env* ',var)
         (= (dynamic-env* ',var) ,val)
         ,@body
         (= (dynamic-env* ',var) ,g-old-val))))
  
  arc> (dynamic-env*-let x 10 (foo))
  x = 10
  5
  arc> (dynamic-env* 'x)
  5
I'm not sure if this addresses your threading issue though. This probably looks a lot like your code except that dynamic-env-let uses the table where your w/config uses the global scope.

And of course, if you were using this all the time you'd want to choose better names. Your fingers wouldn't be happy having to type "dynamic-env-let" very often. :P