when will SAM conversion will be implemented in Ko...
# announcements
o
when will SAM conversion will be implemented in Kotlin? I mean for Kotlin SAM interfaces
👍 2
o
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
By the looks of it it's not a priority, the issue has been open for years..
a
Unless you need to interact with Java use a
typealias
a
@Olekss what advantage does a SAM Interface have over a
(T) -> R
?
h
@Olekss You don't need them in Kotlin, because Kotlin has proper function types.
a
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
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
@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
but it's not an interface
a
so we are back at the question, what advantages does a SAM-Interface have over a simple
(T) -> R
?
d
you can implement an interface, you can convey intent in the naming of it (and its method)
a
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
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
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
i'll have to get back to this after work 😬
👍 1
k
@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
what makes it hard to use from java? isn't
(T) -> R
just a
Function<T, R>
?
l
Kotlin SAM lambda conversion is expected in Kotlin 1.3. It has the advantages of not having autoboxing when not inline
a
so it should be possible to call a functional type with a lambda in java just fine, right?