[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: for



Pete hit the nail on the head.  for is a special form because it does lexical
binding, which can't be done from a primitive, because the primitives don't
have the same lexical scope as their arguments.  (this is what makes lexical
scope lexical, of course.)

local could, in fact, be implemented as a primitive, but we wanted the syntax
to match let.  in fact, here's local as a function
	fn localfn cmd var value { local ($var = $value) $cmd }
or, implemented w/o use of the local syntactic form
	fn localfn cmd var value { 
		let (old = $($var)) {
			catch @ e {
				$var = $old
				throw $e
			} {
				$var = $value
				$cmd
			}
			$var = $old
		}
	}

similarly, a version of for which does dynamic (local) binding is
	fn localfor cmd var values {
		while {!~ $#values 0} {
			local ($var = $values(1))
				$cmd
			values = $values(2 ...)
		}
	}

my apologies if this code doesn't quite work; it all is possible,
which is the point.

paul