Actual parameters to by-name formal parameters are always packaged into a so-called "thunk" (the terminology goes all the way back to Algol in the 1960s) which makes it possible for there to be zero, one or more (any number) of evaluations of the expression that comprises the actual argument. The bit about multiple evaluations is significant if there are side-effects in that expression.
The specific use of that "reference" has to do with the fact that code that will execute in the called function will have a side-effect on a local variable (literally, specifically a var
) in the calling method. That is why the IntRef
is involved. A "ref" of one sort or another (depending on the type of the var
that gets referenced in the actual parameter expression) will always be used in that circumstance. If no var
is involved, the value is simply copied to the thunk.