zsh-workers
 help / color / mirror / code / Atom feed
* Should (t)path = array-unique-special work this way
@ 2016-02-05 13:45 Sebastian Gniazdowski
  2016-02-05 17:15 ` Bart Schaefer
  0 siblings, 1 reply; 10+ messages in thread
From: Sebastian Gniazdowski @ 2016-02-05 13:45 UTC (permalink / raw)
  To: Zsh hackers list

Hello

typeset -U path
PATH="$PATH:/component/nr1"
path+="/component/nr2"
PATH="/component/nr2:$PATH"
print "${(t)path}"
declare -p path


Outputs:
array-unique-special
typeset -a path
path=( /component/nr2 /usr/local/bin /usr/bin /bin /usr/games
/component/nr1 /component/nr2 )

Tested on zsh-5.0.8 and 5.2

Best regards,
Sebastian Gniazdowski


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Should (t)path = array-unique-special work this way
  2016-02-05 13:45 Should (t)path = array-unique-special work this way Sebastian Gniazdowski
@ 2016-02-05 17:15 ` Bart Schaefer
  2016-02-05 17:34   ` Sebastian Gniazdowski
  2016-02-14 10:04   ` Sebastian Gniazdowski
  0 siblings, 2 replies; 10+ messages in thread
From: Bart Schaefer @ 2016-02-05 17:15 UTC (permalink / raw)
  To: Zsh hackers list

On Feb 5,  2:45pm, Sebastian Gniazdowski wrote:
}
} typeset -U path
} PATH="$PATH:/component/nr1"
} path+="/component/nr2"
} PATH="/component/nr2:$PATH"
} print "${(t)path}"
} declare -p path

I'm not really sure what question(s) you're asking.

Aside:
 Although it works to append to an array with path+="/component/nr2",
 it's really not good form.  Get used to path+=("/component/nr2") and
 you'll have much less trouble later.

Back to main topic:

} Outputs:
} array-unique-special

So far so good.

} typeset -a path
} path=( /component/nr2 /usr/local/bin /usr/bin /bin /usr/games /component/nr1 /component/nr2 )

For 5.3, this will change to

typeset -a path=( /component/nr2 /usr/local/bin /usr/bin /bin /usr/games /component/nr1 /component/nr2 )

Other things you might be asking about:

Q: Why is nr2 repeated when path is declared -U ?
A: Because it's repeated in $PATH.  Array uniqueness of tied arrays is
   only applied when assigning to the array; when assigning to the tied
   scalar, the array faithfully copies the value of the scalar.  If this
   were not the case, you could end up with a feedback loop (scalar is
   changed -> array uniquifies -> scalar changes again -> etc.).  This
   in turn is because scalars don't have a "uniqueness" property, so if
   the user is explicitly assigning to the scalar then we presume it is
   expected to reflect the value so assigned.

Q: Why doesn't -U appear in the "declare -p" output?
A: Good question.  It doesn't appear for non-special arrays either.  It
   may be intentional because -U isn't a POSIX-supported option, but I
   suspect it's just an accidental omission.


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Should (t)path = array-unique-special work this way
  2016-02-05 17:15 ` Bart Schaefer
@ 2016-02-05 17:34   ` Sebastian Gniazdowski
  2016-02-05 17:46     ` Peter Stephenson
  2016-02-14 10:04   ` Sebastian Gniazdowski
  1 sibling, 1 reply; 10+ messages in thread
From: Sebastian Gniazdowski @ 2016-02-05 17:34 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Zsh hackers list

On 5 February 2016 at 18:15, Bart Schaefer <schaefer@brasslantern.com> wrote:
> Other things you might be asking about:
>
> Q: Why is nr2 repeated when path is declared -U ?
> A: Because it's repeated in $PATH.  Array uniqueness of tied arrays is
>    only applied when assigning to the array; when assigning to the tied
>    scalar, the array faithfully copies the value of the scalar.

That was my question. The path has *-unique in type, this could be
viewed as a guard that no repetition is possible, but the guard is
avoided by assigning to PATH. That's from one point of view
unexpected.

Side, one lesson is not to rely on attached content and always include
plain question.

Best regards,
Sebastian Gniazdowski


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Should (t)path = array-unique-special work this way
  2016-02-05 17:34   ` Sebastian Gniazdowski
@ 2016-02-05 17:46     ` Peter Stephenson
  2016-02-05 18:19       ` Peter Stephenson
  0 siblings, 1 reply; 10+ messages in thread
From: Peter Stephenson @ 2016-02-05 17:46 UTC (permalink / raw)
  To: Sebastian Gniazdowski, Zsh hackers list

On Fri, 05 Feb 2016 18:34:06 +0100
Sebastian Gniazdowski <sgniazdowski@gmail.com> wrote:
> That was my question. The path has *-unique in type, this could be
> viewed as a guard that no repetition is possible, but the guard is
> avoided by assigning to PATH. That's from one point of view
> unexpected.

It wasn't your question, but you can of course make PATH unique too, and
I suppose an additional lesson is that for least surprises you might
want always to do

typeset -U path PATH

pws


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Should (t)path = array-unique-special work this way
  2016-02-05 17:46     ` Peter Stephenson
@ 2016-02-05 18:19       ` Peter Stephenson
  0 siblings, 0 replies; 10+ messages in thread
From: Peter Stephenson @ 2016-02-05 18:19 UTC (permalink / raw)
  To: Zsh hackers list

On Fri, 5 Feb 2016 17:46:24 +0000
Peter Stephenson <p.stephenson@samsung.com> wrote:
> On Fri, 05 Feb 2016 18:34:06 +0100
> Sebastian Gniazdowski <sgniazdowski@gmail.com> wrote:
> > That was my question. The path has *-unique in type, this could be
> > viewed as a guard that no repetition is possible, but the guard is
> > avoided by assigning to PATH. That's from one point of view
> > unexpected.
> 
> It wasn't your question, but you can of course make PATH unique too, and
> I suppose an additional lesson is that for least surprises you might
> want always to do
> 
> typeset -U path PATH

The behaviour doesn't look like it's documented, at least not in the
obvious place.  The language might be a bit clunky for a shell manual.

diff --git a/Doc/Zsh/builtins.yo b/Doc/Zsh/builtins.yo
index fb630a7..a15f4c2 100644
--- a/Doc/Zsh/builtins.yo
+++ b/Doc/Zsh/builtins.yo
@@ -1980,6 +1980,11 @@ item(tt(-U))(
 For arrays (but not for associative arrays), keep only the first
 occurrence of each duplicated value.  This may also be set for
 colon-separated special parameters like tt(PATH) or tt(FIGNORE), etc.
+Note the flag takes effect on assignment, and the type of the
+variable being assigned to is determinative; for variables with
+shared values it is therefore recommended to set the flag for
+all interfaces, e.g. `tt(typeset -U PATH path)'.
+
 This flag has a different meaning when used with tt(-f); see below.
 )
 item(tt(-Z) [ var(n) ])(


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Should (t)path = array-unique-special work this way
  2016-02-05 17:15 ` Bart Schaefer
  2016-02-05 17:34   ` Sebastian Gniazdowski
@ 2016-02-14 10:04   ` Sebastian Gniazdowski
  2016-02-14 10:19     ` Sebastian Gniazdowski
                       ` (2 more replies)
  1 sibling, 3 replies; 10+ messages in thread
From: Sebastian Gniazdowski @ 2016-02-14 10:04 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Zsh hackers list

On 5 February 2016 at 18:15, Bart Schaefer <schaefer@brasslantern.com> wrote:
>
> Q: Why doesn't -U appear in the "declare -p" output?
> A: Good question.  It doesn't appear for non-special arrays either.  It
>    may be intentional because -U isn't a POSIX-supported option, but I
>    suspect it's just an accidental omission.

Wanted to write a snapshot feature, basically declare -p > snapshot;
setopt typesetsilent; source ./snapshot, but the omission of -U is
quite a blocker. Other interesting thing is that typeset -a a doesn't
fully redefine a, uniqueness still holds if it was present.

% typeset -Ua a
% a=( a a )
% echo ${a[@]}
a
% declare -p > snapshot
% source ./snapshot
% a=( a a )
% echo ${a[@]}
a
% echo ${(t)a}
array-unique
% unset a
% source ./snapshot
% echo ${(t)a}
array
% a=( a a )
% echo ${a[@]}
a a

Best regards,
Sebastian Gniazdowski


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Should (t)path = array-unique-special work this way
  2016-02-14 10:04   ` Sebastian Gniazdowski
@ 2016-02-14 10:19     ` Sebastian Gniazdowski
  2016-02-14 14:34     ` Daniel Shahaf
  2016-02-14 18:44     ` Bart Schaefer
  2 siblings, 0 replies; 10+ messages in thread
From: Sebastian Gniazdowski @ 2016-02-14 10:19 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Zsh hackers list

also:

% echo ${(t)ZPLG_COMPLETIONS_DIR}
scalar
% echo ${parameters[ZPLG_COMPLETIONS_DIR]}
scalar-hideval
% declare -p ZPLG_COMPLETIONS_DIR
typeset ZPLG_COMPLETIONS_DIR

It's declared with typeset -gH

Best regards,
Sebastian Gniazdowski


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Should (t)path = array-unique-special work this way
  2016-02-14 10:04   ` Sebastian Gniazdowski
  2016-02-14 10:19     ` Sebastian Gniazdowski
@ 2016-02-14 14:34     ` Daniel Shahaf
  2016-02-14 18:44     ` Bart Schaefer
  2 siblings, 0 replies; 10+ messages in thread
From: Daniel Shahaf @ 2016-02-14 14:34 UTC (permalink / raw)
  To: Sebastian Gniazdowski; +Cc: Zsh hackers list

Sebastian Gniazdowski wrote on Sun, Feb 14, 2016 at 11:04:02 +0100:
> On 5 February 2016 at 18:15, Bart Schaefer <schaefer@brasslantern.com> wrote:
> >
> > Q: Why doesn't -U appear in the "declare -p" output?
> > A: Good question.  It doesn't appear for non-special arrays either.  It
> >    may be intentional because -U isn't a POSIX-supported option, but I
> >    suspect it's just an accidental omission.
> 
> Wanted to write a snapshot feature, basically declare -p > snapshot;
> setopt typesetsilent; source ./snapshot, but the omission of -U is
> quite a blocker. Other interesting thing is that typeset -a a doesn't
> fully redefine a, uniqueness still holds if it was present.

If the parameter already exists, 'typeset' will modify it rather than
replace it.  For example, 'typeset x=foo; readonly x; echo $x' prints
'foo' rather than an empty string.

You might need to call 'unset' before calling 'typeset'?  I'm not sure
whether that has any edgecases to beware of.


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Should (t)path = array-unique-special work this way
  2016-02-14 10:04   ` Sebastian Gniazdowski
  2016-02-14 10:19     ` Sebastian Gniazdowski
  2016-02-14 14:34     ` Daniel Shahaf
@ 2016-02-14 18:44     ` Bart Schaefer
  2016-02-15  8:25       ` Sebastian Gniazdowski
  2 siblings, 1 reply; 10+ messages in thread
From: Bart Schaefer @ 2016-02-14 18:44 UTC (permalink / raw)
  To: Zsh hackers list

On Feb 14, 11:04am, Sebastian Gniazdowski wrote:
}
} Wanted to write a snapshot feature, basically declare -p > snapshot;
} setopt typesetsilent; source ./snapshot, but the omission of -U is
} quite a blocker.

This doesn't work in the general case anyway, because the output of
"typeset -p" (aka "declare -p") doesn't properly reflect local scopes.
That is, if you're in a local scope, sourcing the "declare -p" output
will create all the variables at that local scope, not at the scopes
where they were originally defined.

In fact it's impossible to correctly reconstruct the local and global
scopes because of the runtime dynamic scoping rules.


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Should (t)path = array-unique-special work this way
  2016-02-14 18:44     ` Bart Schaefer
@ 2016-02-15  8:25       ` Sebastian Gniazdowski
  0 siblings, 0 replies; 10+ messages in thread
From: Sebastian Gniazdowski @ 2016-02-15  8:25 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Zsh hackers list

On 14 February 2016 at 19:44, Bart Schaefer <schaefer@brasslantern.com> wrote:
> On Feb 14, 11:04am, Sebastian Gniazdowski wrote:
> }
> } Wanted to write a snapshot feature, basically declare -p > snapshot;
> } setopt typesetsilent; source ./snapshot, but the omission of -U is
> } quite a blocker.
>
> This doesn't work in the general case anyway, because the output of
> "typeset -p" (aka "declare -p") doesn't properly reflect local scopes.
> That is, if you're in a local scope, sourcing the "declare -p" output
> will create all the variables at that local scope, not at the scopes
> where they were originally defined.
>
> In fact it's impossible to correctly reconstruct the local and global
> scopes because of the runtime dynamic scoping rules.

Assuming the feature is for interactive use, what can be the issues
with scopes? I started a new project, worked around limitations of
declare -p with $parameters:

https://github.com/psprint/zsnapshot

It is called in current scope via alias zsnapshot="source snapshot >
snap", the user is informed that he should source output file at
prompt. Are there any possible issues?

(Started this project to face dirt that e.g. a theme can introduce
into session. Before trying a theme that messes up with precmd() and
PS1, one calls snapshot so that he can revert)

Best regards,
Sebastian Gniazdowski


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2016-02-15  8:26 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-02-05 13:45 Should (t)path = array-unique-special work this way Sebastian Gniazdowski
2016-02-05 17:15 ` Bart Schaefer
2016-02-05 17:34   ` Sebastian Gniazdowski
2016-02-05 17:46     ` Peter Stephenson
2016-02-05 18:19       ` Peter Stephenson
2016-02-14 10:04   ` Sebastian Gniazdowski
2016-02-14 10:19     ` Sebastian Gniazdowski
2016-02-14 14:34     ` Daniel Shahaf
2016-02-14 18:44     ` Bart Schaefer
2016-02-15  8:25       ` Sebastian Gniazdowski

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).