csicar / pskt

Kotlin-backend for PureScript
BSD 3-Clause "New" or "Revised" License
81 stars 5 forks source link

Support for coroutines? #11

Open Sintrastes opened 3 years ago

Sintrastes commented 3 years ago

Hello! First off, I'd just like to say: Thank you for this great project. As someone who loves Kotlin, but also occasionally wants to do some pure-script style things on Android, this is great!

I was just wondering: Are there any plans to support a representation of Kotlin-specific types like suspend functions on the pure-script side? I suppose this could be something like a AsyncEff or SuspendEff function on the purescript side that gets converted into a suspend fun in the backend.

csicar commented 3 years ago

Thank you :)

Yes there are. I had even started implementing something like this. I wanted to map Aff in ps to coroutines in kotlin, but I got stuck at implementing a runtime for Aff.

csicar commented 3 years ago

My attempts at implementing Aff using coroutines can be found here: https://github.com/csicar/pskt-foreigns/tree/coroutines

ATM I'm not looking at trying again, but I'd be happy to get PRs for it ;)

Sintrastes commented 3 years ago

@csicar Am I understanding correctly by a runtime for Aff essentially you're just interpreting all of the "base" functions in Aff as suspend funs in Kotlin (or, actually I see in your code base it looks like you're using (A) -> Deffered<B> -- but as I understand it, that's an isomorphic representation with the semantics of Kotlin).

If so, what are the current open problems with this approach? No guarantees in terms of when I'd actually have the time to dig into it -- but I'd be happy to contribute time permitting.

Sintrastes commented 3 years ago

As a somewhat related question: Have you thought at all about supporting interop with @Composable functions? I'm not sure exactly what the best approach there would be, or how that would look, but it'd be killer to use something like Halogen for organizing components with a compose backend to handle the actual tree diffs/rendering.

csicar commented 3 years ago

Sorry for the late response.

Am I understanding correctly by a runtime for Aff essentially you're just interpreting all of the "base" functions in Aff as suspend funs in Kotlin

Yes, that's right.

If so, what are the current open problems with this approach?

I'm not sure there are any. The problem is just, that a understand neither Aff nor Coroutines well enough to be sure that their behaviour matches. I never really worked with Affs Fiber, so I didn't fully understand the semantics, which made it hard to implement the ffi.

If you'd like to have a try at implementing Aff for pskt, I'd suggest looking at the https://github.com/csicar/pskt-foreigns/edit/coroutines/Foreign.Effect.Aff.kt file and try to fill in the TODO()s. If you need help setting up a dev environment for working on that stuff, I'd be happy to help.

actually I see in your code base it looks like you're using (A) -> Deffered<B>

Yes, I think suspend fun and (A) -> Deferred<B> should be equivalent in Kotlin. I used the second form, because that didn't require modifying the pskt compiler ;)

csicar commented 3 years ago

Have you thought at all about supporting interop with @Composable functions?

Yes, very much. AFAIK Jetpack Compose unfortunately does not use a vdom, but instead does compile-time analysis of your code to insert update to the ui calls at the appropriate places. That could prove challenging to implement in the FFI.

There is also an early attempt at implementing a vdom for normal android widgets, which can be found here: https://github.com/csicar/purescript-android-todomvc/blob/master/ps/src/Main.purs