i have a question about the best way to handle the...
# announcements
h
i have a question about the best way to handle the mutability of a variable using measureTimeMillis. In the next example, I need to store the return object from my method and I want to calculate the time of the process. So I had to create a var object but I don’t want it to be mutable. How do you do ?
c
you could create your own measure time method that returns a pair
Copy code
public inline fun <T> measureTimeAndReturn(block: () -> T): Pair<Long, T> {
        val start = System.currentTimeMillis()
        val result = block()
        return Pair(System.currentTimeMillis() - start, result)
    }
h
@christophsturm thanks a lot, I will have a try.
p
This is one more case where contracts in 1.3 will be useful, so you can just use
val
.
c
how would that work?
p
c
how would the code look then?
even when you are sure that the block gets executed, it still has to be a var. this does not work:
Copy code
val blah
        blah = ""
ah ok it works when it has a type
✔️ 1