From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_EF,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 Received: from zero.zsh.org (zero.zsh.org [IPv6:2a02:898:31:0:48:4558:7a:7368]) by inbox.vuxu.org (Postfix) with ESMTP id 387DE23306 for ; Fri, 1 Mar 2024 19:23:05 +0100 (CET) ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20210803; t=1709317385; b=sLPTXTx4af2BkWZT4CR/eDturElH7D6C4eqJ01fODC7FwT5kxCGErbRtTefcXxm3JgmabAT4YV pV3gQlFubSqXaSsjZ/BVsdPjVr/qv0yPy09P2ub8blZt5ltqHNsPilL05x+cj2lC0LCfJsQZCp +w5jr/KY66lXdKBxGaW7Hj+VEyWwvJev7YAwwRVDREntPNc2ySej3gsOC0wmVTgMSjeSJGsWcP ZsnEauWdvlRFyaASKz5tpu8FEmE+V+kMrIBg0YTXgYNk2xBphlvx+kVq2nToKp7/0TMfEeDe/v Egg67mKi9xC1svJvTdnjUyWonOZQfF+CTjaSi6RpE9E7VQ==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (relay9-d.mail.gandi.net) smtp.remote-ip=217.70.183.199; dmarc=none header.from=chazelas.org; arc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed; d=zsh.org; s=rsa-20210803; t=1709317385; bh=3JzHyBUFyQAe7S4Uz5UyjirVqvhCz9/f51fEnOpvg2g=; h=List-Archive:List-Owner:List-Post:List-Unsubscribe:List-Subscribe:List-Help: List-Id:Sender:In-Reply-To:Content-Type:MIME-Version:References:Message-ID: Subject:Cc:To:From:Date:DKIM-Signature; b=oifiGsf0YXLGHWd1IFCPN8L0ahfQOfdN/mUlLb/aHksn08jyGVmal7rhNRm/2NvPzfRh5rWxGi x+vxbMejxZ5ogPmsuKpmqdcg5LW7UfhQl3Cx85xN4CO1nchf5SE3M6d6HB5myPBEef6/+R0qxl DJJKFp1BnEzV3AcBEwqSGQ3v05eWdzoC23zpgFDWjLFKp9M33/K1XHNvAxnIalQUrzLHbcHkr9 Jn6RqKWDCcUkNEdW2pOlWz6hdet855Z4545EWWmk+QFaRWrmyepVkLk6umaPLtrrAr0psatMJD e8Af/HyWsRz5z3tNkUe1OLgIP5YXAbz3Ohqk/9LiiP2uLQ==; 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:In-Reply-To:Content-Type:MIME-Version :References:Message-ID:Subject:Cc:To:From:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID; bh=ft4CCFNlQcSc/aQDihqBNUIQq156ejOIZ3znl0HjdxU=; b=mEjmHgGsnpYcpDUCQtHFc/Wonp QNxUWCeHGkhLu8zlgrn3HCK7IdbjrbnTwpLs5ascTN5CMs4LLYaoWqAbWjiwBAbfw8e/mkQK8He7x PA8A3sCG1T88sEy3GkdHFNvwARdYFq8wtWZlf/Wqy8Lx7EuSalFgwNC1PIuaIuN6Qi8ldcH7z1kHl KO5MCMHiwCgeEq/UM4blhIkvy2gTfKtAHhk08tziDUtxWIrnGJZZBsnw++Fuahe2/xhtlhr+Bpft3 yZtT9vZCy0z7ZDfsX04ZTqql7SVF04eblaHO3P7DcY8K+PNOhVZ8zfAnHKZCx2Gsf/yoKNJZOtyWw +b+4BgZw==; Received: by zero.zsh.org with local id 1rg7XD-000MGZ-Sg; Fri, 01 Mar 2024 18:23:04 +0000 Authentication-Results: zsh.org; iprev=pass (relay9-d.mail.gandi.net) smtp.remote-ip=217.70.183.199; dmarc=none header.from=chazelas.org; arc=none Received: from relay9-d.mail.gandi.net ([217.70.183.199]:39719) by zero.zsh.org with esmtps (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) id 1rg7Wr-000LyO-VH; Fri, 01 Mar 2024 18:22:43 +0000 Received: by mail.gandi.net (Postfix) with ESMTPSA id C19D3FF804; Fri, 1 Mar 2024 18:22:40 +0000 (UTC) Date: Fri, 1 Mar 2024 18:22:38 +0000 From: Stephane Chazelas To: Bart Schaefer Cc: Zsh hackers list Subject: Re: Up-scope named references, vs. ksh Message-ID: <20240301182238.tpyajwblbam5bxw7@chazelas.org> Mail-Followup-To: Bart Schaefer , Zsh hackers list References: <40726-1676098925.110777@U2kb.d0Pd.I9ml> <20240211070042.4j37hkgjjn3dfjqd@chazelas.org> <20240220210553.g6imt3op6zahz4pa@chazelas.org> <20240221201215.anpjcfav6na55gg6@chazelas.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-GND-Sasl: stephane@chazelas.org X-Seq: 52643 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: 2024-02-28 21:16:13 -0800, Bart Schaefer: [...] > That's where this whole discussion started: It's not good enough to > "record that it's to be global" because if a local of the same name is > later declared, there is no way to insert a new global "above" that, > it's akin to attempting to allocate new space in your caller's stack > frame in C (except there's one stack for each parameter name). The > workaround in shell code is to start with "typeset -g a" (using your > example code) to initialize the top stack frame. The workaround in > underlying C code would be to have "typeset -n r1=a" (again your > example) implicitly create the global $a as early as possible. [...] Couldn't typeset -n ref=var, when it finds that var is not set create one in global scope with a "unset-but-referenced" flag and maybe a reference count associated with it and that can be unset but not removed until the reference count reaches 0 when no nameref point to it? Another (similar) problem: Imagine a function meant to create a variable whose name is passed as argument *as a scalar*: $ ./Src/zsh -c 'f() { typeset -n ref=$1; local var=2; ref=3; }; typeset -A var; f var; echo $var' f: var: attempt to set slice of associative array Doesn't work if the variable was previously set as a hash. Work around would be to unset it first but: $ ./Src/zsh -c 'f() { typeset -n ref=$1; unset ref; local var=2; ref=3; }; typeset -A var; f var; echo $var' $ref ended up pointing to the local var again instead of the one in global scope it was intended to reference. Comparing with ksh93: $ ksh -c 'function f { typeset -n ref=$1; typeset var=2; ref=3; }; typeset -A var=([a]=b); f var; typeset -p var' typeset -A var=([0]=3 [a]=b) $ ksh -c 'function f { typeset -n ref=$1; unset ref; typeset var=2; ref=3; }; typeset -A var=([a]=b); f var; typeset -p var' var=3 -- Stephane