https://kotlinlang.org logo
Title
b

bod

08/03/2019, 3:10 PM
Hello, World! A small API design question: I'm making a lib in Kotlin, and want to make it nice to use from Kotlin and Java. One method takes a date range as a parameter so I used a
Pair<Date>
for it. This way from Kotlin it's nice, you can do
foobar(dateFrom to dateTo)
. But from Java you're going to have to do
foobar(new kotlin.Pair<>(dateFrom, dateTo))
and now you need Kotlin in your dependencies for this to work - not cool. Ideally I'd like to have both
to
and not this Java issue... Ideas?
t

tipsy

08/03/2019, 3:26 PM
I'm making a lib in Kotlin, and want to make it nice to use from Kotlin and Java
write it in java
b

bod

08/03/2019, 3:28 PM
and how do you make
to
work in Java?
m

Mark Murphy

08/03/2019, 3:32 PM
"and now you need Kotlin in your dependencies for this to work - not cool" -- AFAIK a runtime dependency is unavoidable if your library is in Kotlin. If you want to avoid the Kotlin runtime dependency, you would need to implement the library in Java via a
foobar(Date, Date)
method. You could offer a Kotlin wrapper library that has your
foobar(Pair<Date>)
API via an extension function.
b

bod

08/03/2019, 3:35 PM
allright, well 🙂 I'll make a
DateRange
class then. Yeah the extension sounds good. Thanks
l

louiscad

08/03/2019, 3:58 PM
You could also just use two parameters
k

karelpeeters

08/03/2019, 5:31 PM
Also consider using
..
instead of
to
if it's really a range.
1
b

bod

08/03/2019, 9:01 PM
Actually this method already have more parameters, that's why an ad-hoc class is preferable.
..
may be cool indeed yes