https://kotlinlang.org logo
Title
o

Olekss

07/06/2018, 6:55 AM
when will SAM conversion will be implemented in Kotlin? I mean for Kotlin SAM interfaces
👍 2
o

Olekss

07/06/2018, 6:58 AM
I think they work from Java code, but if you have kotlin Interface it does not work
My concern is exactly coz if I want SAM interfaces and use the SAM conversions I need to write them in Java, coz if I write them in Kotlin - won't work
👍 2
d

david-wg2

07/06/2018, 7:05 AM
By the looks of it it's not a priority, the issue has been open for years..
a

alex2069

07/06/2018, 7:09 AM
Unless you need to interact with Java use a
typealias
a

Andreas Sinz

07/06/2018, 7:44 AM
@Olekss what advantage does a SAM Interface have over a
(T) -> R
?
h

hho

07/06/2018, 8:07 AM
@Olekss You don't need them in Kotlin, because Kotlin has proper function types.
a

arekolek

07/06/2018, 8:19 AM
I guess they might be nice on Android, for example if you use data binding, you won't be able to access the lambda type from layout (so it's kind of the "interacting with Java" case, but indirectly).
d

david-wg2

07/06/2018, 8:21 AM
the issue is if you want to create an API that is going to be used from both languages, you can't make it in kotlin. if you want an interface as a parameter, you need to make the API in java
a

Andreas Sinz

07/06/2018, 8:58 AM
@david-wg2 why do you have to create the API in java?
(T) -> R
is just a
Function<T, R>
under the hood and can be used from java just fine
d

david-wg2

07/06/2018, 8:58 AM
but it's not an interface
a

Andreas Sinz

07/06/2018, 8:59 AM
so we are back at the question, what advantages does a SAM-Interface have over a simple
(T) -> R
?
d

david-wg2

07/06/2018, 8:59 AM
you can implement an interface, you can convey intent in the naming of it (and its method)
a

Andreas Sinz

07/06/2018, 9:04 AM
I wouldn't say implementing an interface is an advantage (Functional types are more flexible, because you don't have to implement anything)
and the naming part can be done in kotlin, Interface name will be the
typealias
and the method-name is the argument-name, but doesn't work in java
d

david-wg2

07/06/2018, 9:05 AM
how is it more flexible if it offers a subset of what the other approach offers?
this is a real pain when making an API for both java and kotlin users
a

Andreas Sinz

07/06/2018, 9:13 AM
it is more flexible because everything that has the shape
(T) -> R
can be used, doesn't matter if its a top-level function, "static" function or an instance method. Using a SAM-Interface, the method/function has to be wrapped inside an anonymous object (lambda) that implements the Interface
if you say
subset
, what is possible with an interface but not with a functional type?
d

david-wg2

07/06/2018, 9:17 AM
i'll have to get back to this after work 😬
👍 1
k

karelpeeters

07/06/2018, 10:34 AM
@Andreas Sinz Theoretically functional types are better of course, but in practise they make the API difficult to use from Java. The other alternative are SAM interfaces but those are a pain to use from Kotlin.
a

Andreas Sinz

07/06/2018, 11:07 AM
what makes it hard to use from java? isn't
(T) -> R
just a
Function<T, R>
?
l

louiscad

07/06/2018, 9:26 PM
Kotlin SAM lambda conversion is expected in Kotlin 1.3. It has the advantages of not having autoboxing when not inline
a

Andreas Sinz

07/07/2018, 11:40 AM
so it should be possible to call a functional type with a lambda in java just fine, right?