[Pitch] is case expressions

tldr; Don't introduce a new special-case matching operator/syntax.
Instead, introduce a new property of enums (the base case) which can use existing matching facilities

rather than adding a new matching syntax, how about adding a way of specifying that you're talking about the base enum case and are ignoring the associated value.

using the original example

enum Destination {
  case inbox
  case messageThread(id: Int)
}

one possible syntax for this would be

.messageThread(_)

or perhaps

.messageThread.baseCase

then we have a simple equatable base

.messageThread("Foo").baseCase == .messageThread(_) //true

similarly (trivially)

.inbox.baseCase == .inbox.baseCase

all enums would then have a base case. And this would be useful more generally. I could write the following code

var currentView:Destination = //something
var newView:Destination = //something else
let needToSwitchWindows = newView.baseCase == currentView.baseCase

no new syntax, no new special casing - instead, an extension of enums that let us 'talk about' what their base case is.

This is essentially a language-provided version of the 'parallel enum with no associated values' solution discussed in the original post.

I'm not wedded to the syntax here, there is probably a better way of expressing this - but the idea of exposing a simple way of expressing 'what is the base of this enum' seems powerful to me.

5 Likes