79
/* 2016.07.09 @ 第68回 Cocoa 勉強会関西 */

WWDC 旅行の余談と Swift Open Hours 3 - Swift ラボで聞いてきた話 #cocoa_kansai

Embed Size (px)

Citation preview

/* 2016.07.09 @ 第68回 Cocoa 勉強会関西 */

/**

*/

/* @es_kumagai */

/**

*/

@niwatako

/* 2016/06/13 2016/06/17 */

/* 2016/06/13 2016/06/17 */

// 最初のインデックス var startIndex: Index { get }

// 最後の次のインデックス var endIndex: Index { get }

// 指定したインデックスに該当する要素 subscript (position: Index) -> _Element { get }

// 最初のインデックス var startIndex: Index { get }

// 最後の次のインデックス var endIndex: Index { get }

// 指定したインデックスに該当する要素 subscript (position: Index) -> _Element { get }

// あるインデックスの次のインデックスを取得 func index(after i: Index) -> Index

protocol Indexable {

associatedtype Index : ForwardIndexType

}

protocol Indexable : IndexableBase {

}

protocol IndexableBase {

associatedtype Index : Comparable

}

protocol ForwardIndexType {

func successor() -> Self }

protocol BidirectionalIndexType : ForwardIndexType {

func predecessor() -> Self }

/* 継承関係を簡略化して記載しています */

protocol Comparable : Equatable {

}

protocol Indexable {

var startIndex: Index { get } var endIndex: Index { get } subscript (position: Index) -> _Element { get }

func index(after i: Index) -> Index

}

protocol Indexable {

associatedtype Index : Comparable

var startIndex: Index { get } var endIndex: Index { get } subscript (position: Index) -> _Element { get }

func index(after i: Index) -> Index

}

Swift Lab で聞いてみた!

// 前方移動性をもつ索引型 protocol ForwardIndexType: Equatable // 前後方移動性をもつ索引型 protocol BidirectionalIndexType: ForwardIndexType // どこへも自由に即時移動できる索引型 protocol RandomAccessIndexType: BidirectionalIndexType // コレクションは集積性と、使用する索引を規定 protocol CollectionType { associatedtype Index : ForwardIndexType

// 前方移動性をもつコレクション protocol Collection: Indexable // 前後方移動性をもつコレクション protocol BidirectionalCollection: BidirectionalIndexable // 範囲内を自由に即時移動できるコレクション protocol RandomAccessCollection: RandomAccessIndexable // インデックスに求められるのは比較性のみ protocol Collection { associatedtype Index : Comparable

// このコレクションは前後へ移動可能 struct MyCollection : BidirectionalCollection {

// 索引として文字列を使う typealias Index = String

}

// Int を索引にもつデータ表現は実装の手間が増える struct MyArray<Element> : RandomAccessCollection {

typealias Index = Int

}

// String を索引にもつデータ表現は簡略化される struct Namelist : Collection {

typealias Index = String

@wran_unused_result func calculate(values: Int...) -> Int { … }

// 普通は変数に受ける let answer = calculate(1, 2, 3)

// 変数に受けないと警告になる calculate(1, 2, 3)

// 変数に受けたくないときは、明示的に破棄する _ = calculate(1, 2, 3)

// この書き方で、戻り値を使わないと警告 func calculate(values: Int...) -> Int { … }

// 戻り値を使わなくて良いことを許可したい場合

@discardableResult func apply(effect: Effect) -> Image { … }

@wran_unused_result func calculate(values: Int...) -> Int { … }

// 普通は変数に受ける let answer = calculate(1, 2, 3)

// Playground だと警告にならない calculate(1, 2, 3)

Swift Lab で聞いてみた!

// このようなコードが自動生成される(喩え) let tmp = getValue() print(tmp)

// ただし、この段階では実行時エラー use of unresolved identifier '$builtin_log_with_id' use of unresolved identifier '$builtin_log_scope_exit' use of unresolved identifier '$builtin_log_scope_entry' use of unresolved identifier '$builtin_log_scope_exit' use of unresolved identifier '$builtin_log_scope_exit' use of unresolved identifier '$builtin_log_scope_entry' use of unresolved identifier '$builtin_log_scope_exit' use of unresolved identifier '$builtin_log_with_id'

@_silgen_name ("playground_log_scope_entry") func builtin_log_scope_entry(_:,_:,_:,_:) -> AnyObject? @_silgen_name ("playground_log_scope_exit") func builtin_log_scope_exit(_:,_:,_:,_:) -> AnyObject? @_silgen_name ("playground_log_postprint") func builtin_postPrint(_:,_:,_:,_:) -> AnyObject? @_silgen_name ("DVTSendPlaygroundLogData") func builtin_send_data(_ object:AnyObject?) { @_silgen_name ("GetOutput") func get_output() -> String

/* 再掲 */

Enjoy! Swift/* Thank you */