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

Re: for loop 'bad math expression'




On 2024-02-03 15:52, Bart Schaefer wrote:
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.

0 /usr/share/info 0 % var=abc; let var+=2; echo $var

2

... it looks like the shell is simply throwing away 'abc' and starting afresh with an integer and then incrementing it. And in the manual:

Note that assignment may implicitly change the attributes of a parameter. For example, assigning a number to a variable in arithmetic evaluation may change its type to integer or float, and with GLOB_ASSIGN assigning a pattern to a variable may change its type to an array.

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.

Sure.  I'm hardly surprised it wouldn't be practical, it was the most speculative sort of question.  And there really is no foul since my variable shouldn't have been an integer.  Actually, philosophically I'm opposed to most hand-holding anyway.  With experience I'd probably instantly detect what was going on there.  But when you first bump into it, it seems inexplicable. 


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

Never played with that.  Sounds useful.  As always my 'C-brain' tells me that indiscipline with variables is intolerable.





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