Hello all. Can i build an entire Rest API with arr...
# arrow
r
Hello all. Can i build an entire Rest API with arrow + ktor without using OO (classes, inheritance ...)?
p
Depends on whether ktor requires them or not. In general we don't shy away from implementing interfaces if you consider that inheritance
I've shipped a 20k LOC project in PHP with only a couple of interface implementations so...it should be possible in Kotlin too
😅 1
with or without Arrow
b
I've shipped a 20k LOC project in PHP
Paco, you are a brave, brave man
r
thanks for answer @pakoito
p
@Bob Glamm I'm cheating a little bit because it's in Hacklang...which is PHP + F# 😛
@Rodrigo Silva why do you want to impose those restrictions? and I hope I answered your question. I'm unsure of the context.
r
@pakoito I wanted to program without using OO, only functional, like an Elixir, or Heskell, but using Kotlin.
s
I’ve started doing exactly that, and its been coming out really good. Here is my stack HOCON format with config4k + arrow-kt (IO + Reader) + ktor + JUnit5 (for running every test node parallely) + Docker Test containers. Not a single test contains mocks, and the beauty is, I can write tests from repository layer til right to the routers. Super happy with just arrow-kt
❤️ 1
To add to that I extensively use data classes to hold data and just that, and sealed classes to handle exception, each exception is a domain layer Left hand side object, and right before sending out response, I can just do IO.attempt().map { … } and in that do pattern matching to handle all types of known and unknown exceptions
😍 1
aahhh…. I cannot tell you how much I love arrow
The only problem is, respond(…) method which actually sends response in ktor, can only be called with
ApplicationCall
instance, so I cannot make an extension function on IO to send response, and then it ends up looking like this :
IO.effect { myDomainResponse -> this.call.respond(myDomainResponse) }
which is not nice from purity point of view.
But its life
p
@Satyam Agarwal can I please tweet about your experience? It made me so happy to hear 😄
❤️ 2
s
Hahah, Sure 🙂
r
@Satyam Agarwal could you share your code? I really need a starting point. I recently did a series of articles about ktor + kafka. Now I would like to use Arrow to make everything functional
s
It’s actually company’s property so can’t do that. But I can make some dummy project and share. Just need to wait a little. Too much going on at work. But I’ve been thinking to do that anyway
👍 1
r
that would be cool
t
The only problem is, respond(…) method which actually sends response in ktor, can only be called with
ApplicationCall
instance, so I cannot make an extension function on IO to send response, and then it ends up looking like this :
IO.effect { myDomainResponse -> this.call.respond(myDomainResponse) }
which is not nice from purity point of view.
I've had the same problem. This can be helped with few extension functions.
Copy code
inline fun <reified T : Any> <http://Route.post|Route.post>(noinline body: suspend ConcurrentSyntax<ForIO>.(call: ApplicationCall, args: T) -> Unit): Route =
    post<T>{
        IO.fx {
            body(call, it)
        }.suspendCancellable()
    }

fun ApplicationCall.respondEffect(status: HttpStatusCode, message: Any): IO<Unit> =
    IO.effect { respond(status, message) }