Alexander Ioffe
05/24/2023, 3:00 PM// Scala-equivalent
someone match {
case Customer(Name(first @ "Joe", last), Partner(id)) =>
func(first, last, id)
case Customer(Name(first @ "Jack", last), Organization("BigOrg")) =>
func(first, last)
}
// Decomat in Kotlin
on(someone).match(
case( Customer[Name[Is("Joe"), Is()], Partner[Is()]] )
.then { first, last, id -> func(first, last, id) },
case( Customer[Name[Is("Jack"), Is()], Organization[Is("BigOrg")]] )
.then { first, last -> func(first, last) }
)
Normally it takes much more code to do this sort of thing:
when(someone) {
is Customer ->
if (someone.name.first == "Joe") {
when (val aff = someone.affiliate) {
is Partner -> {
func(someone.name.first, someone.name.last, aff.id)
}
else -> fail()
}
} else if (someone.name.first == "Jack") {
when (val aff = someone.affiliate) {
is Organization -> {
if (aff.name == "BigOrg") {
func(someone.name.first, someone.name.last)
} else fail()
}
else -> fail()
}
} else fail()
}
https://github.com/exoquery/decomat