Hello, This is my first attempt with the new Arrow...
# arrow
p
Hello, This is my first attempt with the new ArrowFx DSL 🙂 https://gist.github.com/PhBastiani/9e494f7fd971848296914c775f052cbe No error/cancellation handling at the moment. Not sure that my tailrecursive
copy
method is the most idiomatic. I noticed a wrong proposal of the IDE: sometimes IntelliJ proposes to remove the
suspend
modifier when it is necessary 😕 Feedback appreciated !
😍 2
s
resourcedCopy
can be a
suspend
function, instead of regular fun returning a
suspend
lambda 🙂
💯 1
p
Yes... fixed ! with the call of
use
in the
main
method... Other point : I don't understand why i cannot write
Copy code
Resource({ fileConsumer(src) }, InputStream::close)
instead of
Copy code
Resource({ fileConsumer(src) }, ::close)
like in your video @Jorge Castillo
j
it's a method reference and I think auto wrapping into suspend doesn't work for those, try to have a suspend function that calls
InputStream::close
and see whether it works
☝️ 1
My example used a stubbed interface just for the example, like:
Copy code
class File {
  suspend fun close(): Unit = TODO()

  companion object {
    fun openFile(path: String): File = File()
  }
}

val resources = listOf(
      Resource({ openFile("path1") }, File::close),
      Resource({ openFile("path2") }, File::close),
      Resource({ openFile("path3") }, File::close)
    )

    val fileContents: List<String> = resources.parTraverse(IOPool) { files ->
      files.use { file -> file.toString() }
    }
s
Yes, the problem there is that it’s not the same signature. There is actually a
Continuation
parameter, so the Kotlin compiler needs to explicitly translate that for you. Which it currently doesn’t support. That’d be a great and simple use-case for a compiler plugin, which I’d love to use! 😄
@PhBastiani the
tailrec suspend fun
is very idiomatic IMO. I’ve been using it in lost of places already in the library, and also in the stuff I’m currently building on top. That’s the cool thing about not having the
IO
wrapper anymore, is that we can use regular
tailrec
. I’m also going to introduce a PR in the next day to expose
trampoline
so you could do the same as
IO
does internally in cases where you cannot write a
tailrec
function.
p
Ho yes, the compiler adds a parameter 👌 Yes the Kotlin
tailrec
works like a charm with your DSL.
Refactored with your suggestions ... wrappers seem to be the right solution to integrate Java classes. Thx