bloodshura
07/10/2017, 2:47 AMgetContent()
, 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:
if (response.isOk) {
val content = response.content!!
}
But I don't feel well at all having to use !!
...agrosner
07/10/2017, 4:14 AMevanchooly
07/10/2017, 7:44 AMresponse.content?.let { ... }
michaelzinn
07/10/2017, 7:45 AMresponse.content?.let { content -> ...
if you want to keep the val’s name.response.isOk and respone.content?.let { content -> {
work?marstran
07/10/2017, 8:14 AMresponse.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.Paul Woitaschek
07/10/2017, 8:21 AMif(response.content != null) ... else ...?
response.content?.let {....}
marstran
07/10/2017, 8:23 AMisOk
might check for more than just response.content != null
though.evanchooly
07/10/2017, 8:40 AMisOk
likely is an http response code check. things can be OK
without a response body.karelpeeters
07/10/2017, 8:54 AMdanneu
07/10/2017, 12:36 PM