I just released a small library called Decomat tha...
# feed
a
I just released a small library called Decomat that does Scala-style muti-level deconstructive pattern-matching for Kotlin in a limited context.
Copy code
// 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:
Copy code
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
👍 1
😮 2