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

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



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
}


If loading happens, this will print a random value. If loading doesn't happen at definition time but only at acces time, then this will trigger an error; SRANDOM will fail to load because it's hidden by a local variable. That sounds OK to me but if you think that this should work, then we have to maintain (even ensure) that loading also happens at definition time.

Let me know what you think. I now know more or less what needs to be done but obviously the code will be different depending on whether we want loading at definition time or not.

Philippe


On Tue, Jun 10, 2025 at 6:18 PM Philippe Altherr <philippe.altherr@xxxxxxxxx> wrote:
At least at present, the only way to find the actual type of the
autoloaded parameter is to load it.  It is possible to have an
autoloaded PM_NAMEREF.

Even though an autoloaded parameter could be a reference that could create a reference loop, I don't think that we need to load it at definition time. If the reference that refers to the autoloaded parameter is ever accessed, that should trigger the loading. If the autoloaded parameter turns out to be a reference, it will go through its own self-reference check and any reference loop will be discovered at that point (even though, I kind of doubt than an autoloaded reference can ever create a loop). So, I think it should always be fine to load only at access time.

Returning to my comment from the other thread, though, I would think
getparamnode_nofollow() does not need to autoload, and that change
could be dropped and just replaced with gethashnode2()?

Iirc, that's what I tried to do but then a test with an autoloaded parameter failed. The problem was that the parameter didn't load, which broke the test. What I didn't know at the time was that there is a bug in the code for references with subscripts. Because of that bug, an access to the reference doesn't trigger the loading of the main variable. In other words, the test was passing thanks to the loading triggered at definition time by getparamnode_nofollow. The bug can be observed with the following example:

zmodload -ap | grep SRANDOM || echo "SRANDOM was loaded"
typeset -n ref=SRANDOM[1,20]
zmodload -ap | grep SRANDOM || echo "SRANDOM was loaded"
zmodload -u zsh/random                                   # Resets SRANDOM to autoload
zmodload -ap | grep SRANDOM || echo "SRANDOM was loaded"
echo $ref
zmodload -ap | grep SRANDOM || echo "SRANDOM was loaded"

Output
SRANDOM (zsh/random)
SRANDOM was loaded
SRANDOM (zsh/random)
zsh/random
SRANDOM (zsh/random)

The statement "echo $ref" should trigger the loading of "SRANDOM" but fails to do so.

I'll have a look and try to fix this bug. Then it should become possible to use just gethashnode2(), which will be much cleaner.

Philippe

 

On Tue, Jun 10, 2025 at 4:59 PM Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
On Mon, Jun 9, 2025 at 10:10 AM Philippe Altherr
<philippe.altherr@xxxxxxxxx> wrote:
>
> I find it strange that the simple fact of defining a reference triggers any loading.

At least at present, the only way to find the actual type of the
autoloaded parameter is to load it.  It is possible to have an
autoloaded PM_NAMEREF.

Returning to my comment from the other thread, though, I would think
getparamnode_nofollow() does not need to autoload, and that change
could be dropped and just replaced with gethashnode2()?


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