https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
p

Patrick Jackson

08/12/2019, 6:16 PM
Anyone know current state of using lambas with a return value of Unit in native? Currently in swift I have to always return a KotlinUnit() from a lambda. I've seen an open issue. Has this changed, or will this change in future versions of KN? i.e. to pass a lamda to a Kotlin function we must do:
Copy code
myKtFun { dostuff()
    return KotlinUnit() }
t

Thomas

08/12/2019, 6:25 PM
If you enable the generics then the return type changes to Void. So you don’t need to add a return statement anymore.
p

Patrick Jackson

08/12/2019, 6:28 PM
I'm using 1.3.41 with generics and still have to use return statements. I suppose that is in a newer release
t

Thomas

08/12/2019, 6:30 PM
I am using 1.3.41 as well and don’t have to add the return to lamdas. Are you sure the generics are enabled correctly using
-Xobjc-generics
?
m

Marc Dietrichstein

08/12/2019, 6:53 PM
I also didn’t not have to return KotlinUnit() anymore with 1.3.41. `-Xobjc-generics`wasn’t enabled in my case.
t

Thomas

08/12/2019, 6:56 PM
Kotlin 1.3.40 has the following in the release notes:
In addition to this, the Kotlin type Unit will turn into Void in the produced frameworks.
It was just below the generics supports so I thought you needed the generics for this. But it looks like it works without the generics as well.
The docs still say you need to add the return. I guess this still needs to be updated? https://github.com/JetBrains/kotlin-native/blob/master/OBJC_INTEROP.md#function-types
p

Patrick Jackson

08/12/2019, 7:00 PM
It is working on 1.3.41 for me now that I go back and clean & rebuild. Thanks Thomas
k

kpgalligan

08/12/2019, 8:31 PM
I was pretty surprised by the generics part and was going to comment, but got distracted …
t

Thomas

08/12/2019, 9:26 PM
@kpgalligan yes, sorry about that. I don't think it is really clear in the 1.3.40 change log if this was in addition to generics or to the whole update. It says "in addition to this..". I thought it referred to the generics part.
k

kpgalligan

08/12/2019, 9:30 PM
Yeah, I can see that getting confused. I was surprised when we updated to 1.3.4x that Unit went away. It’s pretty nice not having to do that.
b

basher

08/12/2019, 9:32 PM
They improved it in many cases in 1.3.41. If you found a case they missed, you should file a GH issue
s

svyatoslav.scherbina

08/13/2019, 7:30 AM
Generics are irrelevant here.
The docs still say you need to add the return. I guess this still needs to be updated?
Yes, thanks.
myKtFun { dostuff()
return KotlinUnit() }
myKtFun
is likely not a function but a function-typed object here.
Unit
is not represented as
Void
when in is that deep. I.e.
() -> Unit
type becomes
() -> Void
, but
(() -> Unit) -> Unit
becomes
(() -> Unit) -> Void
.
p

Patrick Jackson

08/14/2019, 12:55 PM
@svyatoslav.scherbina Ok, yes I still have to
Copy code
return KotlinUnit()
in some situations. My usecase is a
Copy code
typealias Subscriber = () -> Unit
fun subscribe(subscriber: Subscriber) { ... }
Think this will ever go away? or is there some limitation that is blocking
s

svyatoslav.scherbina

08/14/2019, 1:14 PM
Then you either use older version of Kotlin or the usecase is more complex. This
subscribe
case works properly for me. Could you recheck?
p

Patrick Jackson

08/14/2019, 1:20 PM
I see now, yes it is more complex.
Copy code
typealias Subscription = () -> Unit
val subscribe: (Subscriber) -> Subscription
using
Copy code
subscribe  { }
from swift forces me to
Copy code
return KotlinUnit()
s

svyatoslav.scherbina

08/14/2019, 1:32 PM
p

Patrick Jackson

08/14/2019, 1:40 PM
yes, see that sorry...just asking if you think this will likely change in a future version of kotlin. If you think the more complex function-typed objects (such as (
Copy code
() -> Unit) -> Unit)
ever be able to return Void from Swift. Bit of a pain point when using from swift.
s

svyatoslav.scherbina

08/14/2019, 5:17 PM
We have no ETA for this.