io.decomat.Is.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of decomat-core Show documentation
Show all versions of decomat-core Show documentation
DecoMat - Deconstructive Pattern Matching for Kotlin
package io.decomat
class Is private constructor (private val type: Typed, private val valueCompare: ValueCompare): Pattern0(type) {
override fun matches(r: ProductClass): Boolean =
isType(r.value, type.type) &&
when(valueCompare) {
is DoCompare -> r.value == valueCompare.value
is DontCompare -> true
}
companion object {
private sealed interface ValueCompare
private data class DoCompare(val value: R): ValueCompare
private object DontCompare: ValueCompare
fun TypedAs(type: Typed) = Is(type, DontCompare)
fun ValuedAs(type: Typed, value: R) = Is(type, DoCompare(value))
inline operator fun invoke(): Is = Is.TypedAs(Typed())
inline operator fun invoke(value: R): Is = Is.ValuedAs(Typed(), value)
}
}
inline fun IsAny() = Is()