Array of Nested Type: Why Does the Compiler Complain?

后端 未结 3 1551
花落未央
花落未央 2020-12-07 03:43
class ClassA {
    class ClassB {
    }
}
let compiles: [ClassA.ClassB]
let doesNotCompile = [ClassA.ClassB]()

Playground execution failed: MyPlayg

相关标签:
3条回答
  • 2020-12-07 03:58

    As you noted, it works with this syntax:

    let arrayOfClassB: [ClassA.ClassB] = []
    

    but the []() syntax works if we declare a typealias:

    typealias InnerClass = ClassA.ClassB
    let arrayOfAliasesOfClassB = [InnerClass]()
    

    So I'd say it's a bug, let arrayOfClassB = [ClassA.ClassB]() should also work without needing a typealias.

    Update: there's already an opened bug about this at Apple.

    0 讨论(0)
  • 2020-12-07 04:02

    I don't know the reason why the shorthand syntax does not work. However the compiler seems to like the extended syntax

    let list = Array<ClassA.ClassB>()
    
    0 讨论(0)
  • 2020-12-07 04:17

    compiles is an array of type ClassA.ClassB

    In doesNotCompile you are trying to use an array as a function. Didn't you mean:

    let doesNotCompile = ClassA.ClassB()
    
    0 讨论(0)
提交回复
热议问题