9

Why do I get an error when I add enum to generic class:

class TestClass<T>{ 
    enum TestEnum {
        case test
    }  
}

Error:

1.  While type-checking 'ExampleTest' at /Users/xxx/xxx/xx/xx/ExampleTest.swift:11:1
<unknown>:0: error: unable to execute command: Segmentation fault: 11
<unknown>:0: error: swift frontend command failed due to signal (use -v to see invocation)
Command /Applications/Xcode6-Beta3 2.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift failed with exit code 254

But I don't get an error when I do this :

class TestClass{ 
    enum TestEnum {
        case test
    }  
}

or this:

class TestClass<T>{ 
}
4
  • 2
    Your first two examples are exactly identical - is it possible you may have posted the same snippet twice? Commented Jul 22, 2014 at 23:18
  • Compiler bug with the combination of generic class and nested enum. File a bug report and de-nest the enumeration for now as a workaround. Commented Jul 23, 2014 at 1:42
  • @weltraumpirat Thanks, corrected the example. Commented Jul 23, 2014 at 11:14
  • @NateCook Yeah it looks like it, do you see the same behavior when compiling the code? If so, you could add your comment as an answer so I could accept it. Commented Jul 23, 2014 at 11:15

1 Answer 1

20

You can't nest any type inside a generic one and vice versa. In other words, you can't do things like these for classes, structs, and enums:

class Outer<T> {
    class Inner { }
}

and

class Outer {
    class Inner<T> { }
}

and even

class Outer<T> {
    class Inner<T> { }
}

Apple folks explained the reason for the restriction:

It's an implementation limitation. We'll remove the restriction once our compiler and runtime are able to correctly handle types nested in generic contexts.

P.S. Sorry that I post the answer so late but the issue is still there (XCode 6.2).

There was a very similar question, by the way.

Sign up to request clarification or add additional context in comments.

1 Comment

Swift 3.1 and XCode 8.3.2 support this now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.