iswiftapp / iswift

Objective-C to Swift Converter
30 stars 3 forks source link

switch-case block #116

Closed charlieMonroe closed 8 years ago

charlieMonroe commented 8 years ago

This is valid in ObjC/C/C++:

switch (i) {
    case 0: {
        int o = i + 1;
        // Etc.
        break;
    }

...

}

Notice the { } block after : in the case - this forces the compiler to create new code block, in which it is valid to declare new variables. This is normally forbidden since switch in C-based languages is by default a single code block (which is why it is fallthrough by default) and can be chopped up using break.

In Swift, this is completely legal even without the {}:

switch i {
    case 0:
          let o = i + 1
          ...

    case 1:
    ...
}

iSwift currently converts this into a comment "Unimplemented".

drkameleon commented 8 years ago

Good catch. I'm putting that into my to-do list for 2.0. (it's gonna be the biggest release so far. I hope the wait is worth it ;) )

drkameleon commented 8 years ago

Issue fixed as of the upcoming 2.0.


Example:

@implementation aClass

- (void)main {
    switch (i) {
        case 0: {
            int o = i + 1;
            // Etc.
            break;
        }

        case 1: 
            NSLog(@"It's one");
            break;

        default:
            NSLog(@"Another case...");

    }
}

Output:

class aClass {

    func main() {
        switch i {
            case 0:

                var o: Int32 = i+1
                // Etc.

                break

            case 1:

            print("It's one")
            break
            default:

            print("Another case...")

        }

    }

}

P.S. The only issue right now is with the formatting, which remains to be resolved in some later release...

drkameleon commented 8 years ago

Re: https://github.com/drkameleon/iswift/issues/122