`while (value.links.next != null) {` any chance to...
# announcements
d
while (value.links.next != null) {
any chance to smartcast value.links.next to non-null inside the loop? Basically looking for a counterpart to
nullable?.let { it is not null }
r
value.links.forEach { if (it.next != null) {} else {} }.... something like this might work
d
value.links is not a list 🙂
Copy code
while (value.links.next != null) {
    doSomething(value); 
    value = getNextPage(value.links.next) 
}
that's more or less the code I have at hand
r
what about value & value.links ? are they not null values?
only
next
could be null?
v
value
is mutable, so nothing can be smart-casted as a different thread could modify it in the meantime.
👍🏻 1
n
@df maybe use
generateSequence
?
maybe something like:
Copy code
for ((value, next) in generateSequence { if (value.links.next != null) value to next else null }) { ... }
might be more readable with
forEach
instead of a for loop
n
might be a rare good opportunity for tail recursion
d
maybe easier to help with the full code
obv. it's working I just don't like the way it "looks" 😉
n
Copy code
var firstRequest = true

    generateSequence("/api/rest/v1/products") {
        val response = httpClient.get<ResultPage<Product>> {
            url(it)
            header("Authorization", "Bearer ${token.accessToken}")
            if (firstRequest) {
                contentType(ContentType.Application.Json)
                parameter("search", objectMapper.writeValueAsString(search))
                params.forEach { k, v -> parameter(k, v) }
                firstRequest = false
            }
        }
        send(response)
        response.links.next?.href
    }.takeWhile { true }
ideally there should really be a function called
takeAll
instead
g
I prefer this way to handle it:
Copy code
while (true) {
    val value = value.links.next ?: break
    doSomething(value)
}
It makes value immutable, smart cast works as expected, it very flexible One may doesn't like
while(true)
estetically, but on practice I don't see big issue there, especially because it provides many benefits Actual implementation may be different, depending on scope, but it just general idiom, use while(true) + elvis with break to make smart cast work
3
☝️ 1