Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-349] Cannot use nested enums in definition of arrays or as dictionary #42969

Closed
swift-ci opened this issue Dec 23, 2015 · 4 comments
Closed
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-349
Radar rdar://problem/16849958
Original Reporter michaelosity (JIRA User)
Type Bug
Status Resolved
Resolution Done
Environment

Xcode 7.2, Swift 2.1

Additional Detail from JIRA
Votes 1
Component/s Compiler
Labels Bug
Assignee @slavapestov
Priority Medium

md5: 12ab5d036e835351aaf6c2f0e01d2b6d

is duplicated by:

  • SR-2428 Cannot use the short Array constructor syntax when creating an Array of a nested Struct
  • SR-3532 Can't access metatype of an array of nested types using square-bracket syntactic sugar
  • SR-4300 Getting error when declare empty array of nested struct or classes.

Issue Description:

I'm not sure the correct terminology to use here, but I've been playing around with moving my related enumerated types into the types they classify (nested enumerations in classes). When you do this, you need to refer to the enumeration as "ClassName.EnumerationName.Case" which seems to break down when using those in an array or dictionary.

class Class1 {
    enum NestedEnum: Int {
        case A
        case B
        case C
    }
}

class Class2 {
    var objectArray: [Class1.NestedEnum] = [Class1.NestedEnum]()
    var objectDictionary: [Class1.NestedEnum:Class1] = [Class1.NestedEnum:[Class1]]()
}

I've been able to workaround this issue by using a typealias to the enumerated value, but that seems clumsy.

@swift-ci
Copy link
Collaborator Author

Comment by Simon Whitty (JIRA)

Specifically, this only effects initializing an Array with the shorthand syntax.

//Error: invalid use of '()' to call a value of non-function type '[Card.Suit.Type]'
var a = [Card.Suit]()

//Works fine
var b = Array<Card.Suit>()
b.append(.Spades)

//also fine
var c : [Card.Suit] = Array<Card.Suit>()

@ChristopherRogers
Copy link
Contributor

FWIW, I ran into this issue as well, but with nested type aliases instead of enums. So it seems that this issue affects the array & dictionary literal sugar syntax for all nested types.

public struct MyStruct {
    public typealias MyTypeAlias = String
}

// ERROR: Invalid use of '()' to call a value of non-function type '[MyTypeAlias.Type]' (with fix-it to delete '()')
_ = [MyStruct.MyTypeAlias]()

// ERROR: Cannot call value of non-function type '[MyTypeAlias.Type]' (without fix-it)
let _: [String] = [MyStruct.MyTypeAlias]()

_ = [] as [MyStruct.MyTypeAlias] // OK
let _: [MyStruct.MyTypeAlias] = [] // OK
_ = Array<MyStruct.MyTypeAlias>() // OK
_ = [GlobalTypeAlias]() // OK
_ = [String]() // OK

@swift-ci
Copy link
Collaborator Author

swift-ci commented Jan 4, 2017

Comment by Philippe (JIRA)

I ran into this while accessing the metatype of an array of enums nested in a class, like this: [SomeClass.SomeEnum].type. I wrote some examples in this (now marked duplicate) issue if it helps anyone: SR-3532

@slavapestov
Copy link
Member

#9765

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself
Projects
None yet
Development

No branches or pull requests

3 participants