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

Re: suprise with -=

On 10/19/2015 11:35 AM, Bart Schaefer wrote:
On Oct 18, 10:46pm, Ray Andrews wrote:
} That's a bit of a surprise, why is zsh fussier with '-=' than with '+='?

Outside of the (( )), -= is not an assignment operator at all, because
the default is to do either array or string assignment, and there is
no sensible way to "subtract" one array or string from another.

Conversely += is defined to mean "append", so it is a valid operator.

I'd almost call it a bug that [outside of (( )) context] first+=second
does arithmetic when $first is an integer.  It ought to be either an
error or forcibly convert $first to string and append "second".
I've managed to suppress my gag reflex with all that arithmetic stuff--goes back to the need to 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. I see what you mean tho--we can append strings but we can't un-append them (tho one might consider that as " first=${first%$second} " (not sure if I wrote that right, but you get me) ). For now I feel comforted to always use the (( )) because it's explicit and you can mercifully space things: (( first -= second )). Just to comment, 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. We don't have warnings do we? In C, of course, if one is doing something strange the compiler doesn't stop you, but it will warn you. I once had great fun writing an encrypt function that performed arithmetic on strings--the compiler didn't like it, but it did what it was told. Na ... can't have warnings in interpreted stuff, tho
there could be a proofreader ...

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