zsh-workers
 help / color / mirror / code / Atom feed
* Documenting $(( foo )) as similar to $(( ($foo) ))
       [not found]                       ` <CAHYJk3SWzLZnzaHvt109Go46G8LQ0Qb1QBQR8CDn5mbDQGVhdA@mail.gmail.com>
@ 2024-02-16  5:45                         ` Lawrence Velázquez
  2024-02-16 17:40                           ` Bart Schaefer
  0 siblings, 1 reply; 2+ messages in thread
From: Lawrence Velázquez @ 2024-02-16  5:45 UTC (permalink / raw)
  To: Mikael Magnusson, Bart Schaefer; +Cc: zsh-workers

On Sun, Feb 4, 2024, at 9:43 PM, Mikael Magnusson wrote:
> On 2/5/24, Bart Schaefer <schaefer@brasslantern.com> wrote:
>> On Sun, Feb 4, 2024 at 12:49 PM Lawrence Velázquez <larryv@zsh.org> wrote:
>>>
>>> I'm not sure if the zsh manual spells this behavior out explicitly,
>>> but bash and ksh share it.
>>
>> "Arithmetic Evaluation" says (pretty far down after the discussion of
>> operators):
>> ===
>> Named parameters and subscripted arrays can be referenced by name within
>> an arithmetic expression without using the parameter expansion syntax.
>> For example,
>>      ((val2 = val1 * 2))
>> assigns twice the value of $val1 to the parameter named val2.
>>
>> =(and later)=
>>
>> Scalar variables can hold integer or floating point values at different
>> times; there is no memory of the numeric type in this case.
>>
>> If a variable is first assigned in a numeric context without previously
>> being declared, it will be implicitly typed as integer or float and
>> retain that type either until the type is explicitly changed or until
>> the end of the scope.  This can have unforeseen consequences.
>> ===
>>
>> It doesn't explicitly say how "the value of $val1" is determined, but
>> if it were expanded with $val1 you'd get the whole text string which
>> would then be interpreted as arithmetic, so expanding without the $
>> works the same.
>
> This last bit is not 100% true, a parameter will combine into its
> context with $ but not without:
> % a=3+2
> % echo $(( 5 / a )) # 5/5
> 1
> % echo $(( 5 / $a )) # 5/3 + 2
> 3
>
> I think effectively you can think of foo as ($foo), at least in all
> cases I can think of to try.

I just noticed that Bart added the following text to zshmisc(1):

	If the expansion of $val1 is text rather than a number,
	then when val1 is referenced that text is itself evaluated
	as a math expression as if surrounded by parentheses
	`($val1)'.

I assumed that Mikael's comparison of foo with ($foo) was meant to
be an analogy, so I didn't nitpick it at the time, but I worry that
this addition to the documentation could be misconstrued as implying
that code like this ought to be valid:

	% foo='1) + (2'
	% print $((foo))
	zsh: bad math expression: unexpected ')'

...just because this is valid (if ill-advised):

	% print $((($foo)))
	3


-- 
vq


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

* Re: Documenting $(( foo )) as similar to $(( ($foo) ))
  2024-02-16  5:45                         ` Documenting $(( foo )) as similar to $(( ($foo) )) Lawrence Velázquez
@ 2024-02-16 17:40                           ` Bart Schaefer
  0 siblings, 0 replies; 2+ messages in thread
From: Bart Schaefer @ 2024-02-16 17:40 UTC (permalink / raw)
  To: Zsh hackers list

On Thu, Feb 15, 2024 at 9:46 PM Lawrence Velázquez <larryv@zsh.org> wrote:
>
> I just noticed that Bart added the following text to zshmisc(1):
>
>         If the expansion of $val1 is text rather than a number,
>         then when val1 is referenced that text is itself evaluated
>         as a math expression as if surrounded by parentheses
>         `($val1)'.
>
> I assumed that Mikael's comparison of foo with ($foo) was meant to
> be an analogy, so I didn't nitpick it at the time, but I worry that
> this addition to the documentation could be misconstrued

It was my hope/expectation that "as if" would be adequate there.  Note
that in that paragraph we're talking about how val1 works WITHOUT a $
prefix whereas here here you have it WITH a $ prefix:

> ...just because this is valid (if ill-advised):
>
>         % print $((($foo)))
>         3

The latter is only "valid" because of order-of-operations effects, the
inner $foo is expanded before the outer $(( )).

If you can suggest a better way to express "expansion of $val1 is
text" please do.  I rejected "the value of val1" because prior
sentences already refer to "the value" as the result after arithmetic
occurs whereas here we want to refer to the string before arithmetic.


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

end of thread, other threads:[~2024-02-16 17:41 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <4c14e191-0605-4492-9f67-9a5b35ef132b@eastlink.ca>
     [not found] ` <4da0eeb4-4589-4c5d-9b89-a1a22209e18e@eastlink.ca>
     [not found]   ` <CAH+w=7an4ZbDfNxnNkOw-DE07jqarcLXdGg1Tk=uDi_0ZHnY0Q@mail.gmail.com>
     [not found]     ` <CAH+w=7Y6iYdLw-BKepmQVbEKj9d_VLXkq4zWugGpEj1siCt9RA@mail.gmail.com>
     [not found]       ` <7ccf5b82-a37d-47b5-a700-fb1096ab495c@eastlink.ca>
     [not found]         ` <0875ffd7-e3a4-4ddc-9c4b-47e2c593ea4c@eastlink.ca>
     [not found]           ` <CAH+w=7aznm0sBRqz4=Mo4q86oMzKrg0ktSEtFBGQupiWSV7WEg@mail.gmail.com>
     [not found]             ` <f228c732-d237-4597-b260-b28da72fd84b@eastlink.ca>
     [not found]               ` <ecf249c0-3787-463d-bc30-1440fc6b1510@app.fastmail.com>
     [not found]                 ` <ec856c16-59c3-4967-b309-cb0da750354b@eastlink.ca>
     [not found]                   ` <1745fff9-822c-499f-b60e-7248e95438c5@app.fastmail.com>
     [not found]                     ` <CAH+w=7ayzF1D7N-n-bs9y3_zyiYb7WCTnJgg9B8ER7j+fXVdaw@mail.gmail.com>
     [not found]                       ` <CAHYJk3SWzLZnzaHvt109Go46G8LQ0Qb1QBQR8CDn5mbDQGVhdA@mail.gmail.com>
2024-02-16  5:45                         ` Documenting $(( foo )) as similar to $(( ($foo) )) Lawrence Velázquez
2024-02-16 17:40                           ` Bart Schaefer

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).