Array of nested type: why does the compiler complain?

class ClassA {
    class ClassB {
    }
}
let compiles: [ClassA.ClassB]
let doesNotCompile = [ClassA.ClassB]()

Playground execution failed: MyPlayground.playground: 109: 22: error: invalid use of '()' to call a value of non-functional type [ClassA.ClassB.Type] 'let doesNotCompile = ClassA.ClassB ^ ~~

+3
source share
3 answers

As you noted, it works with this syntax:

let arrayOfClassB: [ClassA.ClassB] = []

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

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

Thus, I would say that this is a mistake, let arrayOfClassB = [ClassA.ClassB]()should also work without using types.

Update : there is already an open error about this in Apple.

+3
source

, .

let list = Array<ClassA.ClassB>()
+1

compiles is an array of type ClassA.ClassB

In doesNotCompile you are trying to use an array as a function. You didn’t mean:

let doesNotCompile = ClassA.ClassB()
-1
source

Source: https://habr.com/ru/post/1654743/


All Articles