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

Re: Should defining a reference to an autoload parameter load it?



I will go for NOT loading (which I think also leads to simpler code; no getparamnode_nofollow needed).

Or maybe it's not simpler :-(

I have now a patch that loads references with subscripts only at access time. Thus the following example leads to an error:

() {
  typeset -n ref=SRANDOM[1,20];
  typeset -h SRANDOM=local-variable
  echo START
  echo $ref
  echo END
}

Output:
START
(anon):4: Can't add module parameter `SRANDOM': local parameter exists
(anon):zsh/random:4: error when adding parameter `SRANDOM'
(anon):4: autoloading module zsh/random failed to define parameter: SRANDOM

However the following script still loads SRANDOM at definition time:

() {
  typeset -n ref=SRANDOM;
  typeset -h SRANDOM=local-variable
  echo $ref
  echo $SRANDOM
}
Output:
4184917433
local-variable

Preventing this one from loading SRANDOM at definition time would be rather difficult at the moment. It might become easier once my simplification patches are in but for the time being, I will rather ensure that the first script also loads SRANDOM at definition time. Because surely both scripts should at least behave the same way.

If we desire the non loading behavior, we can revisit this once the code is smaller and simpler.

I will send the patch once I have cleaned it up and added tests. It may only be in 2-3 days as I might be busy the next 2 days.

Philippe




On Tue, Jun 10, 2025 at 10:47 PM Philippe Altherr <philippe.altherr@xxxxxxxxx> wrote:
Oh, interesting, I didn't know that specials couldn't be hidden without the -h option.

Is it expected that the following doesn't trigger an error?

zmodload -u zsh/random
() {
  typeset SRANDOM=foo
  echo $SRANDOM
}
Output:
foo

Apparently, for nested variables, the read-only error is only triggered if the special was previously loaded. If you do the same at the top-level or add an "echo $SRANDOM" before the "typeset", then you get the error.

Regarding reference definitions, do you have any preference about whether they should trigger loading? If not, I will go for NOT loading (which I think also leads to simpler code; no getparamnode_nofollow needed).

Philippe


On Tue, Jun 10, 2025 at 9:17 PM Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
On Tue, Jun 10, 2025 at 11:24 AM Philippe Altherr
<philippe.altherr@xxxxxxxxx> wrote:
>
> Here is an example that will behave differently depending on whether loading happens or not at definition time:
>
> () {
>   typeset -n ref=SRANDOM[1,20];
>   typeset SRANDOM=local-variable
>   echo $ref
> }

This is one of the things that the upscope() your patches removed from
createparam() was checking for:  a special hidden by a local.  (Might
nevertheless have had some of the edge cases wrong.)  If loading
happens (or if the named reference declaration is omitted), you should
see this:

(anon):2: read-only variable: SRANDOM

because the default for locals with the same name as specials is to
retain the special behavior.  This is currently broken with your
patches applied, including the explicit case:

   local +h SRANDOM

This is avoided by

  local -h SRANDOM

in which event your analysis about named reference dereferencing does apply.


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