zsh-workers
 help / color / mirror / code / Atom feed
From: "Lawrence Velázquez" <larryv@zsh.org>
To: "Mikael Magnusson" <mikachu@gmail.com>,
	"Bart Schaefer" <schaefer@brasslantern.com>
Cc: zsh-workers@zsh.org
Subject: Documenting $(( foo )) as similar to $(( ($foo) ))
Date: Fri, 16 Feb 2024 00:45:53 -0500	[thread overview]
Message-ID: <c71935ff-91ef-427d-9bee-472b99bb14b4@app.fastmail.com> (raw)
In-Reply-To: <CAHYJk3SWzLZnzaHvt109Go46G8LQ0Qb1QBQR8CDn5mbDQGVhdA@mail.gmail.com>

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


       reply	other threads:[~2024-02-16  5:46 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [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                         ` Lawrence Velázquez [this message]
2024-02-16 17:40                           ` Bart Schaefer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=c71935ff-91ef-427d-9bee-472b99bb14b4@app.fastmail.com \
    --to=larryv@zsh.org \
    --cc=mikachu@gmail.com \
    --cc=schaefer@brasslantern.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).