A Scala 3 (Dotty) enum match/case expression example

Here’s a little Scala 3 (Dotty) enum match/case expression example:

// a Scala 3 (Dotty) enum
enum Suit {
    case Clubs, Diamonds, Hearts, Spades
}

object DottyEnumTest1 extends App {

    import Suit._
    
    // an enum in a match expression
    def printEnum(suit: Suit) = suit match {
        case Clubs => println("clubs")
        case Diamonds => println("diamonds")
        case Hearts => println("hearts")
        case Spades => println("spades")
    }

    printEnum(Clubs)
    printEnum(Diamonds)
    printEnum(Hearts)
    printEnum(Spades)
    
}

This works as of August 22, 2019 with Dotty 0.17. I don’t think it worked earlier this summer, as I had made a note to ask if Scala 3 enums would eventually work with match expressions. So this is cool.

May, 2020 update with Dotty 0.24

As a quick update in May, 2020, you can now write that same Scala 3 code this way with Dotty 0.24:

enum Suit:
    case Clubs, Diamonds, Hearts, Spades

@main def dottyEnumTest =

    import Suit._

    def printEnum(suit: Suit) = suit match
        case Clubs => println("clubs")
        case Diamonds => println("diamonds")
        case Hearts => println("hearts")
        case Spades => println("spades")

    printEnum(Clubs)
    printEnum(Diamonds)
    printEnum(Hearts)
    printEnum(Spades)

I don’t know how other people feel, but I really enjoy using the new Scala/Dotty syntax without curly braces.