The problem is that you think of blocks as if they were thunks, as if they were code snippets. This is not true. { a; b; c } { a; b; c } not part of the code that can be passed.
So, how should you reason about implications? Actually, how should you reason about representations that are implicit transformations. Views apply to the value that needs to be changed. In your example, the value
{ println(1) println(2) }
passed to setRunnable . The value of a block is the value of its last expression, so it passes the result from println(2) to setRunnable . Since this Unit and setRunnable requires a Runnable , an implicit search is found, so println(2) is passed to the grossly incorrect name blockToRunnable .
The bottom line is there, and this is the advice that I have given many times on Qaru (many people try to do the same) to get the following in your head:
THERE ARE NO BLOCKS IN SCALA.
There are functions, but not blocks.
Technically, this statement is wrong - there are blocks in Scala, but they are not what you think of them, so just completely remove them from your mind. You can find out which blocks in Scala are the last, from scratch. Otherwise, you should try to get them to work in ways they donโt do, or to conclude that everything works in a certain way when they work differently.
source share