Arc Forumnew | comments | leaders | submitlogin
Dyn: dynamic binding that works with exceptions, continuations, and threads (github.com)
3 points by CatDancer 5644 days ago | 8 comments


1 point by rntz 5643 days ago | link

Anarki has a very similar thing, lib/dynvar.arc, authored by yours truly. It defines 'let* and 'with* and 'withs* (by analogy with the corresponding lexically-binding forms) instead of just 'dynamic, and 'dynvar instead of 'declare-dynamic, although the semantics differ a little - my forms can be used with any variable, and essentially amount to setting a given place within the dynamic extent of a certain region of code and resetting it afterwards, hence the threadsafety issues; and 'dynvar sets a variable to a thread-local cell. So it separates the issue of threadsafety from that of dynamic scope, which may or may not be handy.

There is a small bug in the code as written - it uses 'protect instead of scheme's 'dynamic-wind, so a multiply-resumed continuation could break it.

-----

2 points by CatDancer 5643 days ago | link

I have a new version where I use your 'dynvar name, but I haven't been able to upload it yet today because github is overloaded.

-----

1 point by CatDancer 5643 days ago | link

Am I using the Anarki version here correctly?

  arc> (dynvar a 3)
  3
  arc> (a)
  3
  arc> (let* (a) 5 (a))
  5

-----

1 point by rntz 5643 days ago | link

Ayup. That's the idea.

-----

1 point by CatDancer 5643 days ago | link

You may be interested in my "autocall" patch, that I'm renaming to "defvar" and will be writing some documentation for soon. It extends the Arc compiler to allow you to provide your own implementation for a global variable, so that you can type "a" instead of "(a)".

-----

1 point by rntz 5643 days ago | link

Sounds vaguely similar to symbol macros, which are a more general facility in which you can make a given symbol expand to arbitrary code (rather than just the invocation of a given symbol). I'd be more interested in seeing a hack to implement those in arc; you can do some interesting things with them

In this case, I honestly don't find that writing the extra parens is much of a bother to me (and I have used dynvars quite a bit in one of my projects) - it actually helps remind me "oh hey, this is a dynamic variable". It's like an enforced naming convention; "(foo)" is just as succinct as 'foo, really.

Edit: dammit, that's supposed to be 'foo surrounded by stars - the CL dynamic variable naming convention. Unfortunately it's misinterpreted as italics. Is there some way around this?

-----

1 point by shader 5643 days ago | link

pg's convention in the arc code is to just have one star after the name, instead of one on either side. Less typing, and it doesn't conflict with italics ;)

-----

1 point by CatDancer 5642 days ago | link

New version is up at http://hacks.catdancer.ws/dyn.html

This uses rntz's dynvar name to declare a dynamic variable, and I'm trying out the "ac-scheme" name for the syntax to tunnel a Scheme expression through the Arc compiler.

-----