In Addition to our current compiler plugins we’d l...
# arrow-meta
i
In Addition to our current compiler plugins we’d like to present
autofold
, which will give you an automatic fold function over valid GADT/ ADT in vanilla Kotlin at compile time.
Copy code
sealed class A

data class B(val x: Int) : A()
data class C(val x: Int) : A()
object D : A()

sealed class Expr<out A, B>
data class Const(val number: Double) : Expr<Nothing, Double>()
data class Sum<C>(val e1: Expr<Int, C>, val e2: Expr<Int, C>) : Expr<Int, C>()
object NotANumber : Expr<Nothing, Nothing>()
Copy code
fun syntheticExprFold(): Int =
    Sum(
      e1 = Const(3.0),
      e2 = Const(2.1)
    ).fold(
      { c: Const -> c.number.toInt() },
      { _: Sum<Double> -> 0 },
      { 94 }
    )

fun syntheticAFold(): Int =
    B(44).fold(
      { b: B -> b.x * b.x },
      { c: C -> c.x - c.x },
      { 0 }
    )
Here is the code: https://github.com/47deg/arrow-meta-prototype/blob/master/compiler-plugin/src/main/kotlin/arrow/meta/autofold/AutoFoldPlugin.kt arrow
Autofold
is currently on hold, because my last commit introduced breaking changes to out idea support