matching a protocol value in multiple patterns is not yet supported; use separate cases instead


(Zhao Xin) #1

I encounter this error today. "matching a protocol value in multiple
patterns is not yet supported; use separate cases instead"

Sample code:

import Foundation

protocol NameProtocol {

    var name:String { get }

}

struct Cat:NameProtocol {

    let name:String

}

struct Dog:NameProtocol {

    let name: String

}

enum AnimalType {

    case catType(cat:NameProtocol)

    case dogType(dog:NameProtocol)

}

struct Animal {

    let type:AnimalType

    func printName() {

        switch type {

        case .catType(let animal), // matching a protocol value in multiple
patterns is not yet supported; use separate cases instead

            .dogType(let animal):

            print(animal.name)

        }

    }

}

let cat = Cat(name: "kitty")

let catType = AnimalType.catType(cat: cat)

let animal = Animal(type: catType)

animal.printName()

I am wondering which proposal this is? And will it be implemented in Swift
4.0? I search the error as keywords, but didn't get the expected results.

Zhao Xin


(Jordan Rose) #2

It’s mostly just a bug. In Swift 3.1 this code was accepted but did the wrong thing at run-time. So we put in the error message until we can get around to fixing that, which at this point is unlikely to happen for Swift 4. There’s no new proposal needed, just more time to work on it.

Sorry for the inconvenience,
Jordan

···

On Jul 18, 2017, at 18:17, Zhao Xin via swift-users <swift-users@swift.org> wrote:

I encounter this error today. "matching a protocol value in multiple patterns is not yet supported; use separate cases instead"

Sample code:

import Foundation

protocol NameProtocol {
    var name:String { get }
}

struct Cat:NameProtocol {
    let name:String
}

struct Dog:NameProtocol {
    let name: String
}

enum AnimalType {
    case catType(cat:NameProtocol)
    case dogType(dog:NameProtocol)
}

struct Animal {
    let type:AnimalType
    
    func printName() {
        switch type {
        case .catType(let animal), // matching a protocol value in multiple patterns is not yet supported; use separate cases instead
            .dogType(let animal):
            print(animal.name)
        }
    }
}

let cat = Cat(name: "kitty")
let catType = AnimalType.catType(cat: cat)
let animal = Animal(type: catType)
animal.printName()

I am wondering which proposal this is? And will it be implemented in Swift 4.0? I search the error as keywords, but didn't get the expected results.