1

I have expression like this in Kotlin: fun getSomething() = create()

How to check in IntelliJ IDEA (Android Studio) during debug what value does function return? If I just put breakpoint it does not show expression result.

UPD. I understand that if I rewrite code using a temporary variable I could debug it:

fun getSomething() { 
  var tmp = create()
  return tmp
}

But how to do it without rewriting code?

anber
  • 3,463
  • 6
  • 39
  • 68
  • 1
    Put the breakpoint at the line the calls the method: `val x = getSomething()` or if possible at the return line of `ctreate()`. – forpas Jan 02 '19 at 16:02
  • There's a similar question asked [here](https://stackoverflow.com/questions/5010362/can-i-find-out-the-return-value-before-returning-while-debugging-in-intellij) – Yoni Gibbs Jan 02 '19 at 16:10
  • You should write an unit test for it, or at least `check(getSomething() == create()` – Omar Mainegra Jan 02 '19 at 16:20
  • Did my answer help? – Willi Mentzel May 29 '20 at 22:46
  • @WilliMentzel I appreciate your help, but it doesn't answer my question. – anber May 30 '20 at 08:09
  • @anber But you can use "evaluate expression" as described in the second part of my answer. Use a break point at the line where you define getSomething, then evaluate it. The temp variable was just meant to illustrate the point. Pls clarify. – Willi Mentzel May 30 '20 at 14:36
  • @WilliMentzel evaluate expression is not that what I want cause method call could take some time and may cause some side effects, but looks like no other solution, so I'll accept your answer – anber May 30 '20 at 19:25

1 Answers1

0

Put the breakpoint after the line which calls the function / assigns the return value like this:

fun getSomething(i: Int) = 5 * i

fun main(args: Array<String>) {
    val x = getSomething(5) // (1)

    println(x) // (2)
}

(1) If you put the breakpoint here, x won't be assigned yet

(2) if you want to know the value of x, put the breakpoint on a line after the assignment (println is just used as an example here)

If you want to test what getSomething returns for different values of i,

highlight it and right-click in the editor window when the debugger stops at the breakpoint and choose "Evaluate Expression" (or hit Alt+Shift+8 on Windows or Linux)

In the window that opens you can evaluate any code which is in scope.

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121