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.3 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 21838 invoked from network); 11 Feb 2023 07:46:05 -0000 Received: from zero.zsh.org (2a02:898:31:0:48:4558:7a:7368) by inbox.vuxu.org with ESMTPUTF8; 11 Feb 2023 07:46:05 -0000 ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20210803; t=1676101565; b=bkS3hSzm3HUH6QHaeTLZvSRUcWII1yhin0BZ5jmkxRRg9j6VoZoBZwpLXdHBy5GVc1vXefgLC7 unNzlEAHKL88grPGaFYpkhwSrAAsY6bMB4t2Ll/0ECFYRvBuNkEeL2z8Eb6RKKyOMPVPItnjat jrCpuNaWif68Fi0mKt9m77cDzbpJbIAvFyZ01UYDq4EKIhQBe95S8FOMX/fcPZTBv+oy+7F5Rq vzz617h5LtYobeTAixe4lIyp4gYHrLtutul489C3Lq5m+ULdEZBDZUGPsWLIKPM1EQjS4WFNZR HCs9iOlGhv5WxfMUzQicWFXcSgZtY7nEXbfUptqQBb9pQg==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (mail-ej1-f53.google.com) smtp.remote-ip=209.85.218.53; dkim=pass header.d=brasslantern-com.20210112.gappssmtp.com header.s=20210112 header.a=rsa-sha256; dmarc=none header.from=brasslantern.com; arc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed; d=zsh.org; s=rsa-20210803; t=1676101565; bh=oL/OaKNiG80iXzjoMoUHujEX/uxwN+ysJogVocaQBZY=; h=List-Archive:List-Owner:List-Post:List-Unsubscribe:List-Subscribe:List-Help: List-Id:Sender:Content-Type:Cc:To:Subject:Message-ID:Date:From:In-Reply-To: References:MIME-Version:DKIM-Signature:DKIM-Signature; b=BNmM5IHtJuc1te26qtbSlPKI81WHrJwfQho3ue2itfR/iKXWQ+K0oDP+p7jMgr705vx0/+PbYJ ks9bwwLDaSRRGPv4DOcEusRYQv0As6ocMmCqVXwbblY7PQiUjL9ktJYmkrmJ03FN7hjEiiSe8n Ftd8hDenNbEy7mxXROAKOK3cfzE+5COFLiC4kvYoZZNx2WpSS441xKQxz78lEycGEDNQI6uxzC 2n5dU57wZsldso7cDVvwnPUr5GJcgZ74jwER4ELgeVHFqWAL05XuRdGbrpeGl2f6bo84MdYDe6 /0SIIx2i6YuKoyv3tAFCl3gG/aa+CSqcKXFuMSFLSwPIDw==; 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:Content-Type:Cc:To:Subject:Message-ID :Date:From:In-Reply-To:References:MIME-Version:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID; bh=UNtcr5RrmR/6fievB2c7sDX1Hn4i34Wkj12wISjoD9Y=; b=q8w81m1mzEvvkoIYAxEtxku3gc O9rlHNKNUUT6O/s6Dj4tX7HzK0CcGN2lwZojPb22xVWGSQBySXr5yiXLEURWhBe0CTJAyl20J05SE F7tlDofiF26/3ch73buMDnlb3Pwq1/6imxsFrhC4Jnh4OMTmek/+OyRMnEn0J7F5wR6FG2Bntxi5E 54q/b0pVUykVKUXesoCA0hg3SrlGbgu+Qit3fIkUCHQnv7xhFdtzi5lLZXfxxKz1801F2RtNUEivm QJmAtA7ng183fwkOD/h5JsMbGhY6UOXNr7i0U+n1aubgY8VEdjk/8L9yKO/zlyuSY7zXQ0Dfx5iKX 12G0OLDg==; Received: by zero.zsh.org with local id 1pQkaC-000GoZ-Ud; Sat, 11 Feb 2023 07:46:05 +0000 Authentication-Results: zsh.org; iprev=pass (mail-ej1-f53.google.com) smtp.remote-ip=209.85.218.53; dkim=pass header.d=brasslantern-com.20210112.gappssmtp.com header.s=20210112 header.a=rsa-sha256; dmarc=none header.from=brasslantern.com; arc=none Received: from mail-ej1-f53.google.com ([209.85.218.53]:42860) by zero.zsh.org with esmtps (TLS1.3:TLS_AES_128_GCM_SHA256:128) id 1pQkZw-000GVq-9G; Sat, 11 Feb 2023 07:45:49 +0000 Received: by mail-ej1-f53.google.com with SMTP id sa10so20996030ejc.9 for ; Fri, 10 Feb 2023 23:45:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=brasslantern-com.20210112.gappssmtp.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=UNtcr5RrmR/6fievB2c7sDX1Hn4i34Wkj12wISjoD9Y=; b=0WFY3xL/4wlh4MguRFuXRIeWhv+E8fbF4NCV/iShYNq7mGuJ9sFqfulISvo9RBKIB+ ElVqp+JRL6kqCaYUcn+h0k7wc0MQLJXb76Ig1t1t/KytiWvbRt4PKSIFlVsFX96nZ5j0 gxSW6WyI3/MbQ8vMRlrGVLEn3zgVprwBqqEhPd2FIC0obaGzGSRzcP74G2CDS5wUN64t 9i2ovvNn0RJzCw5BdefJQQvjARXQhp/JAanmLhskTChh9yumxv/fENa4nqGtwSSRLuFG iKlAXPw+hTbxTUpig0APtHGiwCI5Y6lhnY1l6CsISfaKgUYYP9gqdezpnfIIEQJ6KduR ApLg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=UNtcr5RrmR/6fievB2c7sDX1Hn4i34Wkj12wISjoD9Y=; b=Vj3JhXbIVhg+HThaHUMUiYaKzuZYayoNbqWtz2QJksL8e6rW0X18zJaFIJnqCL2ybd q65eHPDCnU4cDu81wGItqLJ3Kydh7Xd4BgMP73IhHgGJeb+TL9IPEmoknArKBVQ66uHU HVwAzKy2VMZ7pEr2N2b1zywhKUGIykRr197BVA3gRqeByzbCrGZkSe1zPpFmbqUMUSI2 EMeVfiiE504HN0NH7v6LqTM7t75sOd9+XBeU1owmKYYdV8oa3G27JpLLI8P5e6dKkIyt ekNwZHQea7CWOZRqk1cpzFPFhsiYVmFu9Ym/H6qq9hjkwNlg3xeLALmCnbmD9Z99E1t6 LimQ== X-Gm-Message-State: AO0yUKXvKFDONzSJSV1ymxTzKg5lcbzHRp0URVsHLAvZIF8BKe/vMGVj NrXGwhW4M3bzipfJMlGxTxRM54iV9DWgDC38ZEXOnw== X-Google-Smtp-Source: AK7set8tayWi8dct1G7b1ZaW71H9WP3IbzgUoj+L9jv6ifr27ZKyl5UIG0uXVzOMOwLnU2foATYTUCO8EVcAVYvGDZM= X-Received: by 2002:a17:907:1de6:b0:8ae:cb48:3c80 with SMTP id og38-20020a1709071de600b008aecb483c80mr2008584ejc.7.1676101547807; Fri, 10 Feb 2023 23:45:47 -0800 (PST) MIME-Version: 1.0 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> In-Reply-To: <40726-1676098925.110777@U2kb.d0Pd.I9ml> From: Bart Schaefer Date: Fri, 10 Feb 2023 23:45:36 -0800 Message-ID: Subject: Re: [PATCH 1/3]: Add named references To: Oliver Kiddle Cc: Zsh hackers list Content-Type: text/plain; charset="UTF-8" X-Seq: 51399 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: On Fri, Feb 10, 2023 at 11:02 PM Oliver Kiddle wrote: > > Bart Schaefer wrote: > > On Thu, Feb 9, 2023 at 3:07 PM Bart Schaefer wrote: > > > > > > > Ksh prints "global reference cannot refer to local variable". > > I'd make it a fatal error unconditionally. I don't like it if the target > of a nameref can switch to something else following a return. Making it an unconditional error means that this sort of thing doesn't work: typeset -n ref () { typeset a=A b=B c=C for ref in a b c; do ref=${(L)ref}; done } > 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? Maybe. Might look at that tomorrow. > The mention of WARN_NESTED_VAR raises something else. Surely a reference > should disable that warning: Actually there's an argument that an unexpected up-reference is a really good reason for that warning. A function that intentionally uses an up-reference could turn the option off locally. > > > Relatedly, what should happen on any failed assignment? > > Current code even allows: > > typeset -n ref > ref=ref That's fixed in my sandbox. Patch eventually forthcoming. > I'd suggest that we need the self-reference check whenever assigning to > a reference. But that entails evaluation of a subscript. As I have it right now, self-reference checks do happen at any assignment. That's why I wonder if deleting the offending parameter is OK. Circular references hidden inside subscripts end up expanding to empty string, as do command substitutions with the NO_EXEC trick. > Do we to put recommended good practices in the doc at all as opposed to > it being a terse description of the functionality. I'd prefer it out of > the way with examples rather than adding fluff to the places you list. The Functions chapter was rather too terse. Here's what I've written to add: --- 8< --- Parameters declared by any of the 'typeset' family of commands during the execution of a function become _local_ to the function unless the '-g' option is used. This is the _scope_ of the parameter, which extends dynamically to any other functions called by the declaring function. In most cases, local parameters take the place of any other parameter having the same name that was assigned or declared in an earlier function scope. (See *note Local Parameters::.) A named parameter declared with the '-n' option to any of the 'typeset' commands becomes a reference to a parameter in scope at the time of assignment to the named reference, which may be at a different call level than the declaring function. For this reason, it is good practice to declare a named reference as soon as the referent parameter is in scope, and as early as possible in the function if the reference is to a parameter in a calling scope. A typical use of named references is to pass the name of the referent as a positional parameter. For example, pop() { local -n ref=$1 local last=$ref[$#ref] ref[$#ref]=() print -r -- $last } array=( a list of five values ) pop array prints the word values and shortens $array to '( a list of five )'. There are no local parameters in pop at the time 'ref=$1' is assigned, so 'ref' becomes a reference to 'array' in the caller. --- >8 --- > I'd also consider it bad practice not to always assign a reference when > creating it. That also doesn't work with the for-loop example above (even if the nameref is moved inside the function).