new in-progress KEEP for guards in `when` expressi...
# language-evolution
a
new in-progress KEEP for guards in
when
expressions -> https://github.com/Kotlin/KEEP/issues/371
🔥 5
K 3
❤️ 3
e
Can someone explain how this relates to pattern matching? Alternative design? Is this better somehow? Couldn't the pattern matching variant be even cleaner? I.e. something like
Copy code
fun render(status: Status): String = when (status) {
    Status.Loading -> "loading"
    is Status.Ok(info = []) -> "no data"
    is Status.Ok -> status.info.joinToString()
    is Status.Error(problem = Problem.CONNECTION) -> "problems with connection"
    is Status.Error(problem = Problem.AUTHENTICATION) -> "could not be authenticated"
    else -> "unknown problem"
}
a
this can be seen as one step in the direction of pattern matching, but trying to avoid using positional pattern matching
l
This is supplementary to pattern matching, not a substitution. Useful when you need some side checks beyond patterns. Name based deconstruction in when (general pattern) will be there ultimately.
p
I feel uniformly using
if
is a nicer syntax and less surprising for those coming from similar pattern guard constructs (i.e. rust or scala), and is potentially semantically clearer (and has no confusion with disjunctions)
It also helps avoid conflating what is essentially a very basic pattern in a when-with-subject and a boolean condition - this might become more nuanced with an improved pattern matching capability.
a
@phldavies thanks for the feedback. It would be really useful for us to have this in the GitHub thread instead of here (so we don't lose information from different sources). thank you color
👍 1