Path: csiph.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail From: Kragen Javier Sitaker Newsgroups: comp.lang.forth Subject: dynamic scoping in Forth in one line of code (was Re: coroutines) Date: Fri, 04 Sep 2026 13:04:10 -0300 Organization: Primarily biological and memetic Lines: 27 Message-ID: <87ik4l6m11.fsf_-_@debian> References: <87bjamc5xh.fsf@debian> <874igd9fjt.fsf@debian> <875x0oa6fy.fsf@debian> <87ecfcld9r.fsf@nightsong.com> <87mrtxzz0p.fsf_-_@debian> <6a9a560f$1@news.ausics.net> MIME-Version: 1.0 Content-Type: text/plain Injection-Date: Fri, 04 Sep 2026 16:06:30 +0000 (UTC) Injection-Info: dont-email.me; logging-data="839551"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19BWzWllvm0+r2xIYumERkQ"; posting-host="19f430bbdbc28c507341c3e22c7199b5" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux) Cancel-Lock: sha1:E3srk+SgneAffUXOzr1szdJP62E= sha1:rtbJkMWDR7uytR31Mk+zmTsQ+Hk= sha256:9QbaDBy2Pf7hC2o9mDNGPUgWEwhNhV/yuipe1xBn+lo= sha1:jhWhhyx+ypri4csur6+7t7lGM3A= sha256:g06DbyQx4kb/3bgep1YQndgMVtvDFstMkral8Otf+a0= Xref: csiph.com comp.lang.forth:135555 dxf writes: > I'm more pragmatist, so if it solves an existing problem then I'm interested. > For years I knew how to 'localize' variables. But it was all done manually. > When Albert and Hans showed me how to implement it simply and with auto-restore, > it made my day. > > : ;: >r ; > > : LOCAL ( x adr -- ) r> -rot dup @ over 2>r ! ;: 2r> ! ; > > \\ Example > variable A variable B 8 a ! 7 b ! > > : divide ( a b -- ) > b local a local a @ b @ / . cr ; > > 15 3 divide a ? b ? > > I use it where re-entrancy is required. While I have loadable 'real' locals, > I hardy bother as this is simpler. This seems to provide exactly the same dynamic-scoping functionality as the `let!` I posted, but with a simpler implementation! How would you extend it to support `throw`? Kragen