On 9 Aug., 17:02, Kevin Wright <[email protected]> wrote:
> Well, Java has left-to-right evaluation, except when it doesn't
>
> x + y * z is equivalent to x.+(y.*(z))  NOT (x.+(y)).*(z)

x() + y() * z() is still evaluated as

invoke x
invoke y
invoke z
mul
add

it has nothing to do with operator precedence. The following two
expressions are not equivalent:

(++x) :: (++x :: Nil)
(++x :: Nil).::(++x)

(Obviously Scala does not allow for the ++ operator, but I did not
want to write up a more complicated example.)

> x :: someList is equivalent to someList.::(x)

If both x and someList are identifiers then yes, and it should give
you pause, because that means the following:

If someList does not have a ::-method, an implicit conversion to a
type that does is looked up. If x also needs implicit conversion for
the method to be applicable then the implicit conversion happens in
the same order for both expressions. Once *right-to-left* and once
left-to-right. That's astonishing behavior.

> You also have this little gem in the Java spec:
>
> "The Java programming language also guarantees that every operand of an
> operator (except the conditional operators &&, ||, and ? :) appears to be
> fully evaluated before any part of the operation itself is performed."

Yes, Java is a strict language with the exception of a few language
constructs.

> and this one:
>
> 15.7.5 Evaluation Order for Other Expressions
>
> The order of evaluation for some expressions is not completely covered by
> these general rules, because these expressions may raise exceptional
> conditions at times that must be specified. See, specifically, the detailed
> explanations of evaluation order for the following kinds of expressions:
>
>    - class instance creation expressions
>    - array creation expressions
>    - method invocation expressions
>    - array access expressions
>    - assignments involving array components

With the exception of array creation I'd expect the semantics for
Scala to be similar (due tue them sharing the same infrastructure) or
unspecified.

> The rules for precedence and order of evaluation are rarely as simple as you
> think :)

I think I've got a pretty good handle on it, thanks.

With kind regards
Ben

-- 
You received this message because you are subscribed to the Google Groups "The 
Java Posse" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/javaposse?hl=en.

Reply via email to