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

Re: suprise with -=



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

(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.)

} 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.

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.



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