Help us understand the problem. What is going on with this article?

Swiftでネストした構造体の初期化エラー

More than 1 year has passed since last update.

概要

下記のようにネストした構造体の配列を初期化しようとした時に何故かコンパイルエラーが出たのでメモ。
(Apple Swift version 3.0.2 (swiftlang-800.0.63 clang-800.0.42.1))

ネストした構造体配列の初期化エラー

構造体StructAの中に構造体StructBを定義し、入れ子にする。

ネストした構造体
struct StructA {
    let a: Int

    struct StructB {
        let b: Int
    }

}

上記のStructBの配列を初期化しようとしたときに、
以下の記述だと何故かシンタックスシュガーが機能せず、コンパイルエラーが出る。

StructBの配列を初期化
// NGパターン
let list1 = [StructA.StructB]()
// error: cannot call value of non-function type '[StructA.StructB.Type]'

回避方法

以下記述方法であればコンパイルエラーはでない。

回避方法
// OKパターン
let list2 = Array<StructA.StructB>()

let list3: [StructA.StructB] = []

typealias StructBinA = StructA.StructB
let list4 = [StructBinA]()

参考

stackoverflowを見るとどうやらswiftのバグらしい。
- stackoverflow / Array of Nested Type: Why Does the Compiler Complain?
- stackoverflow / Why can't I instantiate an empty array of a nested class?

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away