[Proposal] Switch Let


(Eduardo Mourey Lopez Ne) #1

Hi

I will like to make the proposal to allow a switch let, similar to a if let, this will avoid having to repeat every variable name

enum Barcode {
    case UPCA(Int, Int, Int, Int)
    case QRCode(String)
}
var productBarcode = Barcode.UPCA(8, 85909, 51226, 3)

//current:
switch productBarcode {
case .UPCA(let numberSystem, let manufacturer, let product, let check):
    print("UPC-A: \(numberSystem), \(manufacturer), \(product), \(check).")
case .QRCode(let productCode):
    print("QR code: \(productCode).")
}
//suggestion:
switch let p = productBarcode {
case .UPCA:
    print("UPC-A: \(p.numberSystem), \(p.manufacturer), \(p.product), \(p.check).")
case .QRCode:
    print("QR code: \(p.productCode).")
}


(Jed Lewison) #2

You can already do something pretty close to what you want:

enum Barcode {
    case UPCA(numberSystem: Int, manufacturer: Int, product: Int, check: Int)
    case QRCode(productCode: String)
}

var productBarcode = Barcode.UPCA(numberSystem: 8, manufacturer: 85909, product: 51226, check: 3)

switch productBarcode {
case .UPCA(let p):
    print("UPC-A: \(p.numberSystem), \(p.manufacturer), \(p.product), \(p.check).")
case .QRCode(let p):
    print("QR code: \(p.productCode).")
}

(Note that you need to label the tuple elements, or use p.0, p.1, etc.)

···

On May 18, 2016, at 8:28 PM, Eduardo Mourey Lopez Ne via swift-evolution <swift-evolution@swift.org> wrote:

Hi

I will like to make the proposal to allow a switch let, similar to a if let, this will avoid having to repeat every variable name

enum Barcode {
    case UPCA(Int, Int, Int, Int)
    case QRCode(String)
}
var productBarcode = Barcode.UPCA(8, 85909, 51226, 3)

//current:
switch productBarcode {
case .UPCA(let numberSystem, let manufacturer, let product, let check):
    print("UPC-A: \(numberSystem), \(manufacturer), \(product), \(check).")
case .QRCode(let productCode):
    print("QR code: \(productCode).")
}
//suggestion:
switch let p = productBarcode {
case .UPCA:
    print("UPC-A: \(p.numberSystem), \(p.manufacturer), \(p.product), \(p.check).")
case .QRCode:
    print("QR code: \(p.productCode).")
}
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution