Reputation: 33033
When I right-click on a return value in the IntelliJ debugger and choose "Evaluate Expression", nothing happens.
I then have to switch to another stack frame and back again to get Evaluate Expression to work again.
This is with Scala code.
How can I evaluate an expression that refers to the return value? The return value's name in the debugger is not a legal identifier.
Upvotes: 1
Views: 854
Reputation: 481
It's not clear what you mean by clicking "on a return value" in the debugger.
If you have a breakpoint set on a particular line, then the execution of that thread stops before the line is executed. But Scala does not assign the return value of a function or block to any particular name, so it's not clear what "the return value's name in the debugger" refers to.
If you are using the recommended implicit-return style,
def repeatUppercase(base: String, count: Int = 3): String = {
val baseUpper = base.toUpperCase
val parts = List.fill(count)(baseUpper)
parts.mkString
}
and you put a breakpoint on the last line of the block, then your debugger will show the names base
, count
, baseUpper
, and parts
, along with their values, when it stops there. If you paste the expression parts.mkString
into the evaluation window, it will evaluate the expression. If you then step forward in the debugger, the return value will be assigned to whatever name it was assigned to in the calling scope (unless the calling scope discards the return value or uses it anonymously).
On the other hand, if you use explicit-return style,
def repeatUppercase(base: String, count: Int = 3): String = {
val baseUpper = base.toUpperCase
val parts = List.fill(count)(baseUpper)
return parts.mkString
}
then you cannot evaluate the statement return parts.mkString
in the expression evaluator because a statement is not an expression! (Also a return statement is syntactically invalid outside a function definition.)
Upvotes: 2