8
8

More than 5 years have passed since last update.

SwiftのGenericsの挙動メモ

Posted at

自分メモです。
Xcode6 Beta4で、Generics周りでコンパイラの挙動が怪しい。
コンパイラのバグなのか仕様なのかよくわからないので、正式版が出たらもう一度コンパイルしてみようかなと思います。

Segmentation Faultになる

このコードは、

class SomeClass<T1> {
    struct ValueInfo {
        let value: T1
    }

    private(set) var array = [ValueInfo]()

    func add(value: ValueInfo) {
        array.append(value)
    }  
}

こんな感じでSegmentation Faultになる。

1.  While emitting IR SIL function @_TFC6petamp9SomeClass3addU__fGS0_Q__FVS0_9ValueInfoT_ for 'add' at /Users/k_morishita/Documents/xcode/hogehoge/hogehoge/Spike.swift:18:5
<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-Beta4.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift failed with exit code 254

無限ループ?

swiftのコンパイラがCPU100%で止まらなくなる。

class SomeClass<T1> {
    struct ValueInfo<T2> {
        let value: T2
    }

    private(set) var array = [ValueInfo<T1>]()

    func add(value: ValueInfo<T1>) {
        array.append(value)
    }   
}

これならOKだった

Nested Type な struct を外に出したらOKでした。

struct ValueInfo<T2> {
    let value: T2
}

class SomeClass<T1> {
    private(set) var array = [ValueInfo<T1>]()

    func add(value: ValueInfo<T1>) {
        array.append(value)
    }
}
8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8