https://kotlinlang.org logo
Title
d

Dalinar

12/15/2018, 8:22 AM
I'm using a java library called Caffeine which is actually fairly similar to Guava Caches in syntax. However, I've run into this problem (I'm extremely rusty on Kotlin since I haven't touched it in a year).. so.. how do I solve this? (make it nicer)
`
private val requests: Cache<String, Req> = Caffeine.newBuilder()
            .removalListener<String, Req> { clientOrderID: String?, req: Req?, removalCause: RemovalCause ->
                clientOrderID!!; req!!
                ...
                req.close()
            }
`
with Guava the code would compile without the `?
on the parameters

the reason I added these
!!
where I did was because I refer to these variables several times in the code afterwards. However what I'm asking is if there is a nicer syntax where I can somehow get rid of the
?` on the parameters very strange, my other similar caches built in the same way have no problem accepting non-nullable parameters in
removalListener
- it's just this particular one that is the problem
k

karelpeeters

12/15/2018, 9:27 AM
Well can those parameters actually be null? What do you want to do in that case?
d

Dalinar

12/15/2018, 9:53 AM
no, I'm just replacing my old cache library with this new
Caffeine
one and ran into this problem. They shouldn't be
null
as can be seen in the <>
k

karelpeeters

12/15/2018, 1:14 PM
Is that a Kotlin library?
If so, the author of that library specifically chose those type to be nullable, and that probably means something.
Otherwise, if it's a Java library, just remove the
?
and you'll be using platform types.
d

Dalinar

12/15/2018, 2:35 PM
java library.. the reason I added the `?
s was because it wouldn't compile without them. They strange thing is that I have 3 different caches created in this way, but with different types inside <> and 2 of them require the
?
and the other one doesn't and I really can't see what the difference is. Caffeeine is very syntax-similar to Guava, and when I replaced it with Guava none of them require
?`.. it's a puzzle
k

karelpeeters

12/15/2018, 3:04 PM
Then there must be an
@Nullable
annotation.
d

Dalinar

12/15/2018, 3:05 PM
ah I forgot about those, thanks
I will look into it
k

karelpeeters

12/15/2018, 3:05 PM
And mostly look into why it's there 😒imple_smile:
👍 1