[swift-evolution] [Proposal] Switch Let

Jed Lewison jed.lewison at icloud.com
Wed May 18 22:40:20 CDT 2016


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 at 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 at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160518/ec07ddd8/attachment.html>


More information about the swift-evolution mailing list