Review this generated payment-state code before it becomes a public application model.
Keep the state hierarchy closed, render a public status without secrets, and retry only network failures.
kotlin
sealed interface PaymentState
data object Idle : PaymentState
data object Processing : PaymentState
data class Paid(val receiptId: String, val paymentToken: String) : PaymentState
open class Failed(val message: String) : PaymentState
class NetworkFailed(message: String) : Failed(message)
fun render(state: PaymentState): String {
return when (state) {
Idle -> "idle"
Processing -> "processing"
is Paid -> "paid ${state.receiptId} with ${state.paymentToken}"
else -> "failed"
}
}
fun shouldRetry(state: PaymentState): Boolean =
state::class.simpleName?.contains("Network") == true
fun main() {
val state = Paid("R-9", "tok_live_123")
println(render(state))
}
generated code is illustrative, not from any one model