Add value-returning match expressions

This commit is contained in:
pavel 2026-08-27 17:56:13 +02:00
commit ab783c33ed
12 changed files with 384 additions and 14 deletions

View file

@ -108,18 +108,18 @@ enum PaymentResult {
}
fun describe(result: PaymentResult): String {
var description = ""
match (result) {
PaymentResult::Accepted(id) -> { description = id }
PaymentResult::Rejected(reason) -> { description = reason }
PaymentResult::Pending -> { description = "pending" }
return match (result) {
PaymentResult::Accepted(id) -> id
PaymentResult::Rejected(reason) -> reason
PaymentResult::Pending -> "pending"
}
return description
}
```
Enum matches must contain each variant exactly once. Variant payload arity is
checked during Gotlin compilation.
checked during Gotlin compilation. A match used as an expression also requires
every arm to return the same type. Block-style statement matches remain
available for side effects.
## Null safety