From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.4 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 32544 invoked from network); 12 Feb 2023 07:39:21 -0000 Received: from zero.zsh.org (2a02:898:31:0:48:4558:7a:7368) by inbox.vuxu.org with ESMTPUTF8; 12 Feb 2023 07:39:21 -0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=zsh.org; s=rsa-20210803; h=List-Archive:List-Owner:List-Post:List-Unsubscribe: List-Subscribe:List-Help:List-Id:Sender:Message-ID:Date:Content-ID: Content-Type:MIME-Version:Subject:To:References:From:In-reply-to:cc:Reply-To: Content-Transfer-Encoding:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID; bh=pj4M+KA1YkKBXwZvHChHXRwViDoMmdfWLvmMilulqA8=; b=FRQWBp6yaaUmQvBRY37riotmO9 TCiVlLK4T9ZCHKaXwOLjQuCYVc58XbE1KUVeaRdofSADyOvHd7HL/x4wCu1oTXQ2nsNkoxK0BwRxS Y/623CsP4ouviKx8NbiXjtrfx00uZ1QuoEeCbm+MPQ2KTd+Hw2fuXV1JZPilob9PFFA4FP+fjpOk3 kZA3BOfhDnuW9SEyfc68UYV7AcqUa2+4ZhUhfxFVev2mvryZWOZQMN7Qfk0yFKGMkEXYo+ixny/dA u0//clbqCt8MLprcyEFWE2Abt8EY3urK2OlWZeSGcTCJc/nfQFg7z02CPyGBuWvafbIt8JpcsgFZh gljYMRlw==; Received: by zero.zsh.org with local id 1pR6xD-000GBi-Cw; Sun, 12 Feb 2023 07:39:19 +0000 Received: by zero.zsh.org with esmtpsa (TLS1.3:TLS_AES_256_GCM_SHA384:256) id 1pR6wu-000FtE-Kv; Sun, 12 Feb 2023 07:39:00 +0000 Received: from [192.168.178.21] (helo=hydra) by mail.kiddle.eu with esmtp(Exim 4.95) (envelope-from ) id 1pR6wt-000NeH-Hw; Sun, 12 Feb 2023 08:38:59 +0100 cc: Zsh hackers list In-reply-to: From: Oliver Kiddle References: <67689-1675827940.088548@BxvG.D9_b.7RzI> <12608-1675903622.800470@Xj82.e3y1.svhG> <66045-1675975796.128039@FBF_.0yMO.Y8fk> <40726-1676098925.110777@U2kb.d0Pd.I9ml> To: Bart Schaefer Subject: Re: [PATCH 1/3]: Add named references MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <90907.1676187539.1@hydra> Date: Sun, 12 Feb 2023 08:38:59 +0100 Message-ID: <90908-1676187539.552165@4_cC.JL2y.vDqw> X-Seq: 51405 Archived-At: X-Loop: zsh-workers@zsh.org Errors-To: zsh-workers-owner@zsh.org Precedence: list Precedence: bulk Sender: zsh-workers-request@zsh.org X-no-archive: yes List-Id: List-Help: , List-Subscribe: , List-Unsubscribe: , List-Post: List-Owner: List-Archive: 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