8. Write a Document Comment
• すべてのメソッドとプロパティ
• MarkDownのSwift方言を使う
/// Returns the first index where `element` appears in `self`,
/// or `nil` if `element` is not found.
///
/// - Complexity: O(`self.count`).
public func indexOf(element: Generator.Element) -> Index? {
29. Unconstrained Polymorphism
• 例えば、以下のオーバーロードのセットを考える:
struct Array {
/// Inserts `newElement` at `self.endIndex`.
public mutating func append(newElement: Element)
/// Inserts the contents of `newElements`, in order, at
/// `self.endIndex`.
public mutating func append<
S : SequenceType where S.Generator.Element == Element
>(newElements: S)
}
ElementがAnyのとき、曖昧になる
(単一のElementがElementのシーケンスと同じ型扱い)
var values: [Any] = [1, "a"]
values.append([2, 3, 4]) // [1, "a", [2, 3, 4]] or [1, "a", 2, 3, 4]?
30. Unconstrained Polymorphism
• 曖昧さを排除するために2番目のオーバーロードを
より明確に命名する:
struct Array {
/// Inserts `newElement` at `self.endIndex`.
public mutating func append(newElement: Element)
/// Inserts the contents of `newElements`, in order, at
/// `self.endIndex`.
public mutating func appendContentsOf<
S : SequenceType where S.Generator.Element == Element
>(newElements: S)
}
31. Conclusion
• Swift 3 API Guidelines Review
• GitHubで公開
• Swift 3 API Guidelinesの成果の一部
• Swift 3インポータ自動適用結果が見れる
➡ Swift 2.xでもある程度意識しながらコードを書いた方がよい
• ただし、ガイドラインはあくまでガイドライン(指針)
• 細かな部分はプロジェクトやグループで決めが必要
• ガイドライン自体がGitHub上に欲しい