Let's say that I have a method declared in a Java ...
# announcements
b
Let's say that I have a method declared in a Java class, called
getContent()
, which returns a String? (it is annotated as
@Nullable
). However, in the same class, there is another method called
isOk()
, which returns a Boolean. If
isOk()
returns
true
, it means that the String returned by
getContent()
will not be null. Is there any way (like a JSR-305 annotation maybe) I can tell the Kotlin compiler about this? For now, I'm doing the obvious:
Copy code
if (response.isOk) {
    val content = response.content!!
}
But I don't feel well at all having to use
!!
...
a
if (response.isOk) { val content = response.content ?: "" }
e
or
response.content?.let { ... }
m
or
Copy code
response.content?.let { content -> ...
if you want to keep the val’s name.
Would
Copy code
response.isOk and respone.content?.let { content -> {
work?
m
You can do
response.takeIf { it.isOk }?.content?.let { content -> ... }
if you need to check
isOk
. But I think I would use the normal if-test, as most people would find it more readable.
p
Response.isOk is kind of obsolete right?
Why not just
if(response.content != null) ... else ...?
Or if you don't have an else statement just
response.content?.let {....}
m
I don't know which library this is from, but
isOk
might check for more than just
response.content != null
though.
e
isOk
likely is an http response code check. things can be
OK
without a response body.
k
d
had the same question yesterday in another channel 😛