Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: [PATCH 1/3]: Add named references



Bart Schaefer wrote:
> > > I'd be
> > > fine with the reference becoming unset at the time of the return if it
> > > refers to a variable that is going out of scope. Can that be done as
> > > part of the same code that drops the local variables?
>
> That's actually a little startling given dynamic scoping.  It leads to this:

While I said "unset", what I actually meant was only dropping the value
part of the reference - returning it to the `typeset -n ref` state.

However:

> Instead let's think for a moment about how the dynamic scoping works.
> "ref" has two properties:  The locallevel at which it was declared,
> and the locallevel at which it prefers to find a referent.  Any time
> you use $ref (or assign to ref) the search starts at the current
> locallevel and proceeds upward until either the preferred level is
> reached or the "topmost" defined parameter is found, whichever comes
> first.

You've persuaded me. It is consistent with dynamic scoping in general.
Perhaps it has further convinced me (not that it needed to) that lexical
scoping is generally better.

Also when following and considering your examples, the following
scenario occurred to me. I can't see a reason to object to this
behaviour and some of the ideas I had in mind would mess with it. Note
that the reference starts out pointing to an unset variable:

  bar() {
    typeset var=hello
    echo $ref
  }
  foo() {
    typeset -n ref=var
    bar
    var=foo
    echo $ref
  }
  unset var
  foo

It'd be surprising if calling bar somehow lost the value of the
reference.

Oliver




Messages sorted by: Reverse Date, Date, Thread, Author