Imran/Malic
08/23/2019, 8:22 AMautofold
, which will give you an automatic fold function over valid GADT/ ADT in vanilla Kotlin at compile time.
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>()
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
arrowImran/Malic
08/23/2019, 11:54 AMAutofold
is currently on hold, because my last commit introduced breaking changes to out idea support