https://kotlinlang.org logo
#getting-started
Title
# getting-started
s

Stylianos Gakis

09/05/2021, 8:24 AM
I am working with collections, doing the classic
map
filter
associate
and whatnot, but I wanted to debug it. In Java streams we can use the “trace current stream chain” while debugging, but it seems to be greyed off in Kotlin. If I use the
asSequence
I get this functionality, but is this the only way? Sometimes a sequence doesn’t make sense to use, and using it just to enable this debugging functionality feels wrong, is this my only option?
e

edrd

09/05/2021, 11:49 AM
Not sure I understood your question, but can you use "trace current stream chain" with Java collections (not streams)?
s

Stylianos Gakis

09/05/2021, 11:54 AM
No that's true, it's limited in streams in Java too. I was just wondering since it works with sequences, if I can also see how my data changes as it passes down the chain while debugging without having to manually re-run the steps one by one. It's simply a bit more inconvenient to do it in this functional style compared to a more procedural list manipulation which would make debugging easier. But as I understand converting to a sequence is my only option, and that's a trade-off I'm still happy with.
j

Jacob

09/05/2021, 2:35 PM
You don't need trace current stream chain when working with lists. Trace current stream chain is great for streams and sequences where the whole chain of operators are applied to one element at a time. When working with lists, one operator is applied to every element in the list. Then the next operator is applied to every element in the list. The list itself changes between each operator. This makes debugging much easier and you don't need a special tool
s

Stylianos Gakis

09/05/2021, 3:39 PM
But when I am debugging, and I want to see the current state of the list without adding print statements etc, how would I do that?
j

Jacob

09/05/2021, 6:39 PM
Go back one stack frame
s

Stylianos Gakis

09/05/2021, 6:58 PM
Okay either I am completely oblivious to something, or I am not explaining well what I mean here. I’ll continue with a full example here. Code looks like this, just wrote this real quick for the sake of explanation, don’t bother with how stupid it may look like.
Copy code
data class Ticket(val movieName: String, val price: Int, val currencyType: String)
fun whateverList(): List<Ticket> {
    return List(1000) {
        Ticket("Name$it", (10..39).random(), listOf("EUR", "USD", "SEK").random())
    }
}
fun main() {
    val itemsList: List<Ticket> = whateverList()
    itemsList
        .map { it.copy(price = it.price * 2) }
        .filter { it.price < 45 }
        .associateBy { it.currencyType }
}
So then when I am debugging, and I am for example right after the
filter
line, how do I know what kind of data I am working with at that point? (First picture attached) Now if I attach asSequence first, I am getting this nice window that even visualises it for me (Second picture attached) And what I am basically asking here, is if I do not convert it to a sequence, is there any way where I can see at this stack frame where I am directly after calling the
filter
function how my current list under manipulation looks like. How would “going back one stack frame” help here, can I attach a watch to something while debugging that will show me this? Watching itemsList is obviously not enough because it’s immutable, I am not changing that, I am only getting a new one back at the end of it after the chain of actions is over.
j

Jacob

09/05/2021, 7:31 PM
If you stepIn to the associateby call then the 'this' object will be a reference to the filtered list
s

Stylianos Gakis

09/05/2021, 7:32 PM
What if I don't have one of those? What if I have another filter in-between, how do I see the state after the first filter and before the second?
j

Jacob

09/05/2021, 8:36 PM
Same thing. StepIn to the second and the state will be 'this'
👍 1
s

Stylianos Gakis

09/27/2021, 5:17 AM
I now remembered after encountering this again what was it that bothered me. I'm usually using method references in my filters or whatever, and you can't step into those and get access to "it".
j

Jacob

09/27/2021, 5:35 AM
Put a break point in whatever method is being referenced and you can see what parameters were passed to the method which is the equivalent of seeing the value of
it
🎉 1
s

Stylianos Gakis

09/27/2021, 8:25 AM
Haha here to save the day once again. If I find some other case in the next months make sure I’ll be back here again 😅
2 Views