【swift】2.2の変更点
概要
Swift 2.2がリリースされました。オープンソース化後の最初のアップデートですね。
Xcode 7.3 on Mac OSX Yosemite 10.11.4で(一部)確認しました。
予約後の一部が引き数名として使えるようになりました
SE-0001: Allow (most) keywords as argument labels
// inはOK func extractWord(prefix: String, in sentence: String) -> Array? { return nil } // inoutはNG // シングルクォートで囲む必要がある func addParameter(name: String, `inout`: Bool) { }
既存コードに対する影響はありません。
意味が明確になる変数を使えるメリットはありそうですが、一部のキーワードはNGのままというのが微妙です。
タプルの比較演算子が追加されました
SE-0015: Tuple comparison operators
let t1 = (50, 165) let t2 = (50, 165) t1 == t2 // ---> true let t3 = (weight: 50, height: 165) let t4 = (height: 165, weight: 50) t3 == t4 // ---> false
既存コードに対する影響はありません。
要素を先頭から順に比較しています。名前は無視されるので、タプルを比較する場合には演算子をオーバーロードする必要はありそうです。
Sequence初期化に制約を追加することができるようになりました
SE-0014: Constraining AnySequence.init
// 変更前 public struct AnySequence: SequenceType { public init< S: SequenceType where S.Generator.Element == Element >(_ base: S) { ... } } // 変更後 public struct AnySequence : SequenceType { public init< S: SequenceType where S.Generator.Element == Element, S.SubSequence : SequenceType, S.SubSequence.Generator.Element == Element, S.SubSequence.SubSequence == S.SubSequence >(_ base: S) { ... } }
既存コードに対する影響はありません。
メリットがよくわかりませn。
型宣言に関連したtype aliasをassociatedtypeに置き換えることなりました
protocol中では、新しく導入したキーワードassociatedtypeキーワードを使うようにします。typealiasはコンパイルエラーになります。
SE-0011: Replace typealias keyword with associatedtype for associated type declarations
// ワーニングが出力されるようになります。 protocol Prot { associatedtype Container : SequenceType typealias Element = Container.Generator.Element // error: cannot declare type alias inside protocol, use protocol extension instead } protocol Prot { associatedtype Container : SequenceType } extension Prot { typealias Element = Container.Generator.Element }
既存コードに対する影響はありません。
Swift3では、typealiasが全面的になくなり意味が明確なキーワード、type, associated, requiredtype, placeholdertypeに置き換わるようです。
typealiasというキーワードがそもそも曖昧だったということですね。Swift3で仕様が変わるようなので多用しない方がよさそうです。
引数ラベルを使った関数ネーミング
SE-0021: Naming Functions with Argument Labels
// 引数が異なる同じ名前の関数がある場合、 extension UIView { func insertSubview(view: UIView, at index: Int) func insertSubview(view: UIView, aboveSubview siblingSubview: UIView) func insertSubview(view: UIView, belowSubview siblingSubview: UIView) } // 同じ型の関数を呼び分けるためにはクロージャーを作成する必要がありました。 let fn: (UIView, UIView) = { view, otherView in button.insertSubview(view, aboveSubview: otherView) } // これが名前付き引数で関数を区別できるようになりました。 let fn = someView.insertSubview(_:at:) let fn1 = someView.insertSubview(_:aboveSubview:)
既存コードに対する影響はありません。
引き数名で区別しないといけない関数名を作ること自体が良くないような気もするのですが...
Objective-Cセレクタがメソッドとして参照できるようになりました
SE-0022: Referencing the Objective-C selector of a method
control.sendAction(#selector(MyApplication.doSomething), to: target, forEvent: event) extension MyApplication { @objc(jumpUpAndDown:) func doSomething(sender: AnyObject?) { … } }
引数ラベルを使った関数ネーミングを使っています。
既存コードに対する影響はありません。
Objective-Cのコードを含むプロジェクトでは便利なのかもしれません。
新しいビルド設定オプションが追加されました
SE-0020: Swift Language Version Build Configuration
#if swift(>=2.2) print("Active!") // --> Active! #else this! code! will! not! parse! or! produce! diagnostics! #endif
既存コードに対する影響はありません。
Cのプリプロセッサに相当するもの。これは便利そうです。
関連情報
- Swift.org - Swift 2.2 Released!
- SE-0001: Allow (most) keywords as argument labels
- SE-0015: Tuple comparison operators
- SE-0014: Constraining AnySequence.init
- SE-0011: Replace typealias keyword with associatedtype for associated type declarations
- SE-0021: Naming Functions with Argument Labels
- SE-0022: Referencing the Objective-C selector of a method
- SE-0020: Swift Language Version Build Configuration