Will it be better if `fallthrough` works with `label` in nested `switch - case`?


(Zhao Xin) #1

I thought below code would work. It didn't.

let count = 1

outerSwitch: switch count {

case 0, 1:

    let buttonId = 0

    switch buttonId {

    case NSAlertFirstButtonReturn:

        print("do something")

    case NSAlertSecondButtonReturn:

        print("do something")

    case NSAlertThirdButtonReturn:

        print("do something")

        fallthrough outerSwitch // error here

    default:

        fatalError()

    }

default:

    print("do extra things")

}

So I have to use `if - else if - else` instead. I think if `fallthrouh`
works with `label`, the code will be more elegant.

Zhaoxin


(Zhao Xin) #2

I manage to use below work around.

let count = 1

switch count {

case 0, 1:

    let buttonId = NSAlertThirdButtonReturn

    let shouldFallthrough = { () -> Bool in

        switch buttonId {

        case NSAlertFirstButtonReturn:

            print("do something")

        case NSAlertSecondButtonReturn:

            print("do something")

        case NSAlertThirdButtonReturn:

            print("do something")

            return true

        default:

            fatalError()

        }

        return false

    }()

    if shouldFallthrough { fallthrough }

default:

    print("do extra things")

}

Zhaoxin

···

On Sat, Oct 15, 2016 at 10:44 AM, Zhao Xin <owenzx@gmail.com> wrote:

I thought below code would work. It didn't.

let count = 1

outerSwitch: switch count {

case 0, 1:

    let buttonId = 0

    switch buttonId {

    case NSAlertFirstButtonReturn:

        print("do something")

    case NSAlertSecondButtonReturn:

        print("do something")

    case NSAlertThirdButtonReturn:

        print("do something")

        fallthrough outerSwitch // error here

    default:

        fatalError()

    }

default:

    print("do extra things")

}

So I have to use `if - else if - else` instead. I think if `fallthrouh`
works with `label`, the code will be more elegant.

Zhaoxin


(Saagar Jha) #3

This seems like a better solution to me. The other one smacks of goto.

···

On Fri, Oct 14, 2016 at 20:52 Zhao Xin via swift-users < swift-users@swift.org> wrote:

I manage to use below work around.

let count = 1

switch count {

case 0, 1:

    let buttonId = NSAlertThirdButtonReturn

    let shouldFallthrough = { () -> Bool in

        switch buttonId {

        case NSAlertFirstButtonReturn:

            print("do something")

        case NSAlertSecondButtonReturn:

            print("do something")

        case NSAlertThirdButtonReturn:

            print("do something")

            return true

        default:

            fatalError()

        }

        return false

    }()

    if shouldFallthrough { fallthrough }

default:

    print("do extra things")

}

Zhaoxin

On Sat, Oct 15, 2016 at 10:44 AM, Zhao Xin <owenzx@gmail.com> wrote:

I thought below code would work. It didn't.

let count = 1

outerSwitch: switch count {

case 0, 1:

    let buttonId = 0

    switch buttonId {

    case NSAlertFirstButtonReturn:

        print("do something")

    case NSAlertSecondButtonReturn:

        print("do something")

    case NSAlertThirdButtonReturn:

        print("do something")

        fallthrough outerSwitch // error here

    default:

        fatalError()

    }

default:

    print("do extra things")

}

So I have to use `if - else if - else` instead. I think if `fallthrouh`
works with `label`, the code will be more elegant.

Zhaoxin

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users