114
/* */

Swift 3.0 の新機能 - 追加・変更まわりだけ、ざっくり紹介 2 #devsap

Embed Size (px)

Citation preview

/*

*/

/**

*/

/* @es_kumagai */

/**

*/

enum Bit : Int, Comparable, RandomAccessIndexType {

case Zero case One }

struct UnsafePointer<Pointee> : Strideable, Hashable {

var pointee: Pointee { get } subscript(i: Int) -> Pointee { get } }

class Item {

// 戻り値を使わないと警告される @warn_unused_result func makeFilter() -> Filter {…}

// 戻り値を使わなくても警告されない func apply(filter: Filter) -> Item {…} }

class Item {

// 戻り値を使わないと警告される func makeFilter() -> Filter {…}

// 戻り値を使わなくても警告されない @discardableResult func apply(filter: Filter) -> Item {…} }

!

let optionalValue: Int! = 100

// 今は ImplicitlyUnwrappedOptional 型として認識 var a: Int!

// こちらは従来通り Optional 型として認識 var b: Int?

struct Value {

init!(source: Int) { … } func doSomething() { … } }

let value = Value(source: 10)

value?.doSomething() Value?

Value!

func add<T: FloatingPoint>(lhs: T, rhs: T) -> T { return lhs + rhs }

enum Item { case binary(NSData) }

let data1: NSMutableData = … let data2: NSData = …

let item = Item.binary(data1)

data1.appendData(data2)

enum Item { case binary(Data) }

var data1: Data = … let data2: Data = …

let item = Item.binary(data1)

data1.appendData(data2)

let pointer: UnsafePointer<Int> = … if pointer != nil {

print(pointer.memory) } else {

}

let pointer: UnsafePointer<Int>? = … if let value = pointer?.pointee {

print(value) } else {

}

// 最初のインデックス 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 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

protocol BooleanType {

var boolValue: Bool { get } }

!, &&, ||

func && (lhs: Bool, rhs: () -> Bool) -> Bool func || (lhs: Bool, rhs: () -> Bool) -> Bool

func ! (a: Bool) -> Bool

if /* comment */!isValid { ... }

value +/* comment */coefficient

!

+/*

optionalArray/* Comment */?.count

value+/* comment */coefficient

?

+

func f: (Type) -> Type = { }

func test() -> (Type) -> Type { }

func test(f: (Type) -> Type) { }

let f: (Type) -> Bool = { value in

}

func something(first: Int, second: Int, third: Int) {

}

something(first: 1, second: 10, third: 3)

_

// ラベル名を label にする func function(label value: Int) {

}

func function(_ label value: Int) {

}

:

@available(*, unavailable, renamed: "NewProtocol") typealias PreviousProtocol = NewProtocol

// 定義では `` が必要 enum DrawingStyle { case `default` case `repeat` case fit }

// 使用時は `` が不要 let style = DrawingStyle.default

// 定義では `` が必要 class Object {

var `default`: Int func `repeat`(_ times: Int) -> String { … } }

// 使用時は `` が不要 let object = Object() let value = object.default

// 定義では `` が必要 enum Evaluate { case `self` case `dynamicType` }

// 使用時も `` が必要 let eval = Evaluate.`self`

// プロトコル型で使う let p: A & B = Value()

// 型引数で使う func doSomething<T: A & B>(_ value: T) { }

struct Value : Equatable { static func == (lhs: Value, rhs: Value) -> Bool {

}

}

// 既定値が指定された関数があったとき func something(a:Int = 0, b:Int = 1, c:Int = 2) { }

// 異なる順番で引数を渡せなくなった something(b: 10, a: 20)

func function(@noescape f: () -> Int) {

}

func function(@autoclosure f: () -> Int) {

}

func function(f: @noescape () -> Int) {

}

func function(f: @autoclosure () -> Int) {

}

func function(f: () -> Int) -> (@noescape (Int) -> Int) -> Int {

return { g in g(f()) }

}

func function() -> (@autoclosure () -> Int) -> Int {

return { (f: @autoclosure () -> Int) in f() } }

// 関数で得た、関数を取る関数に、値をそのまま渡せる let f = function() let result = f(100)

func method(inout result: Int) {

}

func method(inOut result: Int) {

}

func method(result: inout Int) {

}

func method(inOut result: inout Int) {

}

func function() -> (result: inout Int) -> Void {

return { (result: inout Int) -> Void in

result = 700 } }

let f: (inout Int) -> Void = {

return { $0 = 5000 }

}

func something(value: inout Int) {

let noescaped: @noescape () -> Void = { value = 10 }

let escaped: () -> Void = { [value] in print(value) } }

typealias NamedValues<T> = Dictionary<String, T> typealias Vector<T> = (T, T) typealias Filter<T,R> = (T) -> R

switch (value1, value2) {

case let (value?, nil), let (nil, value?): return value

case let (value1?, value2?): return value1 + value2

case (nil, nil): return 0 }