I’m writing a small library for “Scala-like” pattern-matching in when-expressions. It’s not ready yet (especially collections need some more love), and I’m looking for feedback and criticism. Here is how it can look like:
data class Person(val firstName: String, val lastName: String, val age: Int)
val p = Person("Alice", "Cooper", 74)
when(match(p)) {
"Alien"(any, any, any) -> println("Aliens!")
"Person"("Mick", "Jagger", gt(70)) -> println("Mick Jagger!")
"Person"("Alice", "Cooper", any) -> println("Alice Cooper!")
else -> println("I don't know this guy")
}
Also, polluting global space with String.invoke() is pretty aggressive. I think it would be better to provide this operator through DSL. It could be hard to merge this with when though. Maybe something like:
p.match { when (it) {
...
}}
Not ideal, I agree.
edit: Or resign from using when entirely and do something like:
p.match {
"Alien"(...) then { println() }
"Person"(...) then { println() }
...
}
I switched from String.invoke to KClass.invoke. The String version is a little shorter, but I think using a KClass feels a little more precise and safe (and actually simplifies the code). One thing I didn’t think of before is that renaming a class would spell disaster in the String version. Also, having invoke on the lesser used KClass feels not as unsafe as for String.
I thought about a seperate DSL imitating when, but I don’t see a big advantage, and the user has the burden of learning a new syntax, even if it looks similar. The only thing that might be prettier in an own DSL is the capture pattern, and I don’t think it’s worth it.
I thought about something like this, but at this place I don’t know that I need to start from Person, so I could do only something like (Any) -> Any or need type annotations, which is not great. Further, it would probably mean to restructure large parts of the DSL, while [] is totally orthogonal to other features by using an extension method with receiver.
I’ll ponder over this, maybe there is another syntax to make it work, as it would be useful to have.