|
Posted by Oli Filth on 05/28/06 18:37
Rik said the following on 28/05/2006 15:58:
> Oli Filth wrote:
>> Yes, but {+,-} are "related" operations, so you would perhaps expect
>> them to have similar precedence. Equally, {*,/} are "related", but
>> are "unrelated" to {+,-}. If {+,-,*,/} all had the same precedence,
>> things would get messy and unintuitive. Similarly, as {.} is
>> unrelated to any
>> of them, you would expect it to have a different precedence, but
>> instead
>> it's given the same precedence as {+,-}.
>
> "related" is rather undefined here. I could make a case either way why + and
> * are related, and why they are not, depending on what relation you're
> talking about.
Well, {+,-} are intrinsically related as follows:
Adopting mathematical notation here, if we define:
f(x,c) = x + c
g(x,c) = x - c
then clearly:
f(,c) = g(,c)^-1
i.e. they're inverse operations.
A similar intrinsic relationship holds for {*,/}.
No such magic exists for {+,*} as far as I know.
> If one oversimplifies things, one could say adding (concating) strings is a
> lot like adding numbers. Intuitively, you'd think that "hello" + "world" =
> "helloworld" (which in some languages, it is). Because of PHP's ability to
> cast variables on the fly there had to be another operator, so the operation
> either used numerical value or strings. That might be the reason for the
> same precedence. I'm not saying they are the same.
To be honest, this is the only reason I can think of as well, that
concatenation is "a bit like" addition. Except it's not really, due to
the issue of commutativity, and the fact that there's no inverse
operation. Also, this logic implies that {.} is related to {-}, which
clearly isn't the case...
>> Personally, I don't find it a problem in practice, as I
>> instinctively/automatically parenthesise anything where the precedence
>> is hazy or unclear.
>
> It's just a simple fact of knowing/learning that the . has the same
> precedence. Once, when we were little, we learned + & - had the same in
> math, en / & * also the same. How hard is it to learn that . has the same
> precedence as + & -?
I'm not saying it's hard to learn; what I'm saying is that it's
unintuitive, because IMO the choice of its precedence is arbitrary. For
example, the code in my previous post leads to completely unintuitive
behaviour, IMO.
--
Oli
[Back to original message]
|