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

Re: suprise with -=



On 10/19/2015 05:27 PM, Bart Schaefer wrote:
On Oct 19, 12:34pm, Ray Andrews wrote:
}
} [...] declare variable types, which is what I'm trying to do with
} 'integer' and after that I'd expect both increment and decrement to
} behave the same way.

The parser doesn't know that "first" is an integer, or even that it's
a variable that was previously declared.  This is perfectly legal:

     if (( RANDOM % 7 ))
     then integer first
     else declare first
     fi
     first+=second
Then what does 'integer' or 'typset -i' do?
(Which is why I'm inclined to say it ought to always be string context
when not explicitly math context, but it's probably way to late for that.)

Given all these little gotchas, I think you are right. (()) is math, otherwise you're on thin ice. Still tho, surely 'integer' should be a typecast and not changeable, tho of course it's too late to do anything about that. I can't help but wonder what was going on in the minds of the guys who coded the first shells and decided to permit this sort of anarchy, I'm sure they had their reasons but since the shells are written in C, I'd have
expected normal C discipline to set the standard.
} I'd consider it very rude for any forcible conversions to occur.
} Better an error, tho again once one has declared an integer one might
} expect one's operators to behave consistently.

Note:

torch% integer first=1 second=2
torch% first+=(second)
torch% echo $first
1 second

This has silently forced $first to change into an array because of
explicit array context; it did not interpret "(second)" as arithmetic
parens.

Well I guess that's the universal reality of shells, so I just hafta get used to it. Actually I am already more or less used to it.

Also just to mess things up a bit more:

torch% integer first=1 second=2
torch% third=first+second
torch% print $third
first+second
torch% integer third
torch% print $third
3

If something has a string value and you re-declare it integer, it does
math on its string value.  Doesn't happen for array to integer.

Thanks for the examples, these are so educational. I'll play with them for several days.



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