Samuel
Samuel

Reputation: 6136

Why does this Switch statement require parenthesis for 'Switch Value' and 'Compound case'?

I'm pretty new to Swift so still tinkering and finding my way around. I just wondering why I have to wrap the Switch value chicken.breed, chicken.temper and compound case Breed.leghorn, Temper.hilarious around parenthesises. See error below when I remove the parenthesis

Context The goal of the exercise is to increment chickenOfInterestCount whenever interesting chickens were found matching the Breed and Temper values I wanted to check. So I used a Switch statement to perform these

Exercise.swift

var chickenOfInterestCount = 0

for chicken in chickens {

    // check the chicken variables breed and temper enums
    switch (chicken.breed, chicken.temper){
        case (Breed.leghorn, Temper.hilarious):
            chickenOfInterestCount += 1
        default:
            print("No match")
    }
}

print(chickenOfInterestCount)

Chicken.swift

public enum Breed {
    case leghorn, rhodeIsland, silkie, plymouthRock
}

public enum Temper {
    case friendly, grumpy, hilarious
}

public struct Chicken {
    public let breed: Breed
    public let temper: Temper
}


public let chickens: [Chicken] = [
    Chicken(breed: .leghorn, temper: .friendly),
    Chicken(breed: .leghorn, temper: .friendly),
    Chicken(breed: .leghorn, temper: .grumpy),
    Chicken(breed: .rhodeIsland, temper: .friendly),
    Chicken(breed: .leghorn, temper: .hilarious),
    Chicken(breed: .rhodeIsland, temper: .friendly),
    Chicken(breed: .rhodeIsland, temper: .grumpy),
    Chicken(breed: .silkie, temper: .friendly),
    Chicken(breed: .rhodeIsland, temper: .grumpy),
    Chicken(breed: .silkie, temper: .grumpy),
    Chicken(breed: .rhodeIsland, temper: .hilarious),
    Chicken(breed: .leghorn, temper: .friendly),
    Chicken(breed: .silkie, temper: .friendly),
    Chicken(breed: .leghorn, temper: .hilarious),
    Chicken(breed: .plymouthRock, temper: .grumpy),
    Chicken(breed: .leghorn, temper: .grumpy),
    Chicken(breed: .silkie, temper: .grumpy),
    Chicken(breed: .plymouthRock, temper: .friendly),
    Chicken(breed: .leghorn, temper: .friendly),
    Chicken(breed: .leghorn, temper: .friendly),
] 

Error when removing parenthensis

error: 19_EnumsAndSwitch.playground:9:25: error: expected '{' after 'switch' subject expression
    switch chicken.breed, chicken.temper{
                        ^

error: 19_EnumsAndSwitch.playground:10:9: error: 'case' label can only appear inside a 'switch' statement
        case (Breed.leghorn, Temper.hilarious):
        ^

error: 19_EnumsAndSwitch.playground:12:9: error: 'default' label can only appear inside a 'switch' statement
        default:
        ^

Upvotes: 0

Views: 1368

Answers (1)

rmaddy
rmaddy

Reputation: 318824

A switch takes one expression. By putting the two values in parentheses you are creating a tuple and the case statements match on the tuples.

Without the parentheses you are trying to put two comma separated expressions where there can only be one.

When you use a switch on a tuple, the cases allow you to match various combinations of tuples against the one in the switch expression.

For more on this, see the Tuples and Switch section in the Swift book.

Upvotes: 2

Related Questions