Hi everyone I remember reading some time ago that ...
# arrow
a
Hi everyone I remember reading some time ago that Arrow wanted to implement Union types, am I right? Is there something available in 0.12 version? More info about the usecase in the thread
For the sake of demoing functional programming on backend side I wanted to code a super simple backend. It will contain a simple in memory DB This is my current code:
Copy code
private val db = mutableMapOf<String, Product>()
private val rng = Random(System.currentTimeMillis())

suspend fun getProductById(id: String): Either<Union<DbError, GetProductFromDbError>, Product> {
    delay(100) // simulate some DB query
    return when (rng.nextInt(10)) {
        in (0..8) -> db[id]?.right() ?: ProductNotFoundInDb(id).left() // proper DB call
        else -> DbNotResponding.left()  // Simulate a DB failure
    }
}

sealed class DbError
object DbNotResponding : DbError()

sealed class GetProductFromDbError(val id: String)
class ProductNotFoundInDb(id: String) : GetProductFromDbError(id)
As you can see, I’m simulating some errors to showcase the “available error handling” mechanisms of FP. The main idea is that getting a product from DB is either a result or an error (DB error such as connectivity failures or business error such as a non existing product) However, I’m not able to use the
Union
type (not found). Probably I’m missing some dependencies. I’m adding the
arrow-core
,
arrow-syntax
and ” `kapt`ing”
arrow-meta
of version
0.12.0-SNAPSHOT
y
You also need the
arrow-meta-prelude
dependency
a
Thanks! I was able to use Union class 🙂 Is there any documentation available?
r
no docs, we are waiting for compiler plugins to be official and get IDEA support since for Union types to work without explicit constructors like
first
second
etc you need implicit injection which is provided through Arrow meta
👍 2
You can use today the Union types function manually like first or firstN (nullables) but those are suposed to be transparent to users once we have compiler plugin support which would also render those in the IDE as
A | B | C
TLDR, runtime is there even the injection but it only works in the IR backend for the latest version of meta and would redline in your IDE until it has proper compiler plugin support.
❤️ 1
a
Thanks for the explanation! I’ll wait until the compiler plugins get to an official state 🙂
👍 1
As you mention, once the compiler plugins are released its usage will be more transparent. In the meantime, how should we handle
when
scenarios such as
Copy code
class C1
class C2
val union: Union2<C1, C2> = C1().first()

val a = when(union.value) {
    is C1 -> "First"
    is C2 -> "Second"
    else -> "needed by compiler"  // probably will get out once compiler plugins are released
}

val b = when(union) {
    is First<C1> -> "First"   // Cannot check for instance of erased type
    is Second<C2> -> "Second" // Cannot check for instance of erased type
    else -> "needed by compiler"    // probably will get out once compiler plugins are released
}
The first approach (using
union.value
) seems to work but I wonder if I’m doing something wrong in the second approach. Maybe is just that, not enough support for currently available APIs