reverse-country-code icon indicating copy to clipboard operation
reverse-country-code copied to clipboard

.ifPresent() returns Unit in Kotlin

Open DavidHinkelmann opened this issue 1 year ago • 1 comments

Hey I tried to use the reverse country code in Kotlin and found out that the function ifPresent shown in the Read me only returns Kotlin.Unit

but with reverseGeocoder.getCountry(lat,lon).get() worked totaly fine.

is there a reason? Did I understand something wrong?

DavidHinkelmann avatar Jul 18 '24 08:07 DavidHinkelmann

I've never used Kotlin before, but in Java Optional.ifPresent() is void method, i.e. a method that doesn't return anything. It seems reasonable that the method might return Kotlin.Unit when called from Kotlin if Kotlin is a language that always requires methods to return something.

ifPresent() takes a Consumer<Country> as an argument (that's what the country -> {...} code is in the example). My guess is that in Kotlin you should pass in a function with a signature like Country -> Unit as the argument. The function will be invoked only if the Country is present.

bencampion avatar Jul 18 '24 14:07 bencampion