Arc Forumnew | comments | leaders | submitlogin
Special Variables
2 points by jazzdev 6106 days ago | 3 comments
How do I do something like this in Arc?

  (= defx* "foo")
  (def stdx () defx*)
  (def w/stdx (s thunk)
    (let defx* "bar"
       (thunk)))
So that (w/stdx "bar" (fn () (stdx))) returns "bar" instead of "foo".

Here's what I came up with:

  (def stdx () (atomic defx*))
  (def w/stdx (s thunk)
    (atomic
      (let oldx defx*
        (after
          (do (= defx* s)
              (thunk))
          (= defx* oldx)))))
Is there a more elegant way to do this? Like some way to declare defx* as special so it doesn't get stored in closures?


6 points by almkglor 6105 days ago | link

kennytilton built something like this once: http://smuglispweeny.blogspot.com/2008/02/faux-dynamic-bindi...

The elegant way to do this is to hack together a macro for it^^

-----

4 points by kennytilton 6105 days ago | link

I thought about suggesting special variables when pg asked for suggestions, but I plan on sticking with CL so I thought it inappropriate to... open my big yap? :)

-----

1 point by jazzdev 6081 days ago | link

Hiding the ugliness in a macro is great idea.

But the solution isn't scalable in a multi-threaded program. 50 threads that want to call w/stdx would have to be serialized. And only because they share a temporary variable has to be global.

mzscheme solves this with thread-local variables.

-----