> Scala seems to apply the implicit class conversion on the largest
> possible expression, as in the following example:
>
> ----repl----
> scala> class B { def b = { println("bb"); true } }
> scala> class A { def a = { println("aa"); new B } }
> scala> (new A).a.b
> aa
> bb
> res16: Boolean = true
>
> scala> class XXX(b: => Boolean) { def xxx = 42 }
> scala> implicit def toXXX(b: => Boolean) = new XXX(b)
> scala> (new A).a.b.xxx
> res18: Int = 42
> ----repl----
>
> Which part of the specification addresses this behavior? What prevents
> the compiler from converting into val x = (new A).a.b; toXXX(x).xxx ?
You don't have a largest/smallest problem here; in fact, what you describe is *almost* equivalent to what the compiler is doing. The exact equivalent would be:
toXXX((new A).a.b).xxx
Because your toXXX method takes a by-name parameter (and the XXX constructor does as well), Scala never needs to invoke the body of the thing being converted in order to yield the result 42. If you took away the '=>' in toXXX or the XXX constructor, you'd see the aa/bb side effects again. You can find out more by Googling 'scala by-name parameters'.
(please forgive me my corporate legal disclaimer)
----------------------------------------
This message is intended exclusively for the individual(s) or entity to
which it is addressed. It may contain information that is proprietary,
privileged or confidential or otherwise legally exempt from disclosure.
If you are not the named addressee, you are not authorized to read,
print, retain, copy or disseminate this message or any part of it.
If you have received this message in error, please notify the sender
immediately by e-mail and delete all copies of the message.