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

Re: for loop 'bad math expression'



On Tue, Jan 30, 2024 at 5:44 AM Ray Andrews <rayandrews@xxxxxxxxxxx> wrote:
>
> BTW Bart, just a post mortem on that: I know there are situations where
> a plain vanilla scalar is promoted to integer

As Lawrence also suggested, I don't believe that happens.  If a
parameter is created inside a math expression it will be assigned an
appropriate numeric type, but the type of an existing scalar is not
changed.

Note "created" here includes the case where the parameter was declared
but later unset and then assigned again in math context.

Parameters may also be converted to and from arrays by assignment, and
that includes converting from an array to a numeric type when assigned
in math context.  Maybe that's what you're thinking of.

> I wonder about an automatic demotion.  The
> situation plainly had nothing to do with arithmetic

Context-awareness doesn't extend that far.  Globbing is already done
and gone by the time "for" assigns its loop variable, nothing tells
"for" where the loop values came from, and shell words on a command
line carry no type information.

> perhaps automatic 'non demotion' would be possible -- zsh might have
> robust defenses against making a mistake.

You might try setopt warn_create_global to detect cases of names "leaking".




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