This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// let box = Box(42) | |
// box as! Box<Bool> // 実行時死ぬ | |
Box(42) as! Box<Bool> // コンパイルエラー | |
なぜ!!!!!!!!!!!!!!!!!!!!!?????????????????????????????? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var a = [1,2,3] | |
a.append(contentsOf: [4,5]) | |
print(a) // [1,2,3,4,5] // 見ての通り、[4,5]の配列がflattenされた後、追加されている!!!!!! これの理解がのちのち超大事!!! | |
なぜなら、`Array#flatMap(_:)<SegmentOfResult : Sequence>` は、その内部で `Array.append(contentsOf_:)` を使っているため、 | |
`append(contentsOf_:)` を理解しておかないと、挙動を追う時ハマるから!! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 見ての通り、arrayLiteral の個数は可変 | |
let x = Array<String>(arrayLiteral: "おとはす", "マリリ") // ["おとはす","マリリ"] | |
let y = Array<Int>(arrayLiteral: 1, 2, 3) // [1,2,3] | |
let z = Array<Int?>(arrayLiteral: nil) // [nil] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
class Man {} | |
class SuperMan: Man {} | |
let m = Man() | |
let n = m as! SuperMan // これは当然無理だ。わかる。だが | |
*/ | |
let m = Optional.some(42) | |
let n = m as! Int // なんでこのサブタイピング関係は通るんだァァァァ!!!!! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
用法1(実際にはEquatableに適合してないが、==はたまたま持ってる)で作ったQueueを... | |
extension Queue where Element: Equatable { | |
static func ==(lhs: Queue<Element>, rhs: Queue<Element>) -> Bool { | |
// `Queue`が内部に持つ要素とその順序がすべて同じなら、同じとみなす | |
return lhs.elements == rhs.elements | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// この通りにやったら出来た | |
https://www.google.ca/search?q=array+gyb+teratail&ie=utf-8&oe=utf-8&client=firefox-b-ab&gfe_rd=cr&dcr=0&ei=KrC-WpquDcHM8ge4zb2ACg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol P {} | |
struct S: P {} | |
extension Array { | |
func hoge() -> String { return "Any" } // ① | |
} | |
// enum or 構造体は、「連想型: enum or 構造体」で使えない。 | |
//extension Array where Element: S { | |
// func hoge() -> String { return "Equal" } // ② |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let a: Int? = 42 | |
let b = a as! Int // 成功するか??? => した | |
print(b) // 42 | |
// コンパイラの解決パターン | |
Int to Optional<Int> is [value-to-optional] | |
// つまりこれ...暗黙の型変換「じゃな」くない...??? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Animal {} | |
class Cat: Animal { | |
func pero() {} | |
} | |
let animals: [Animal] = [Cat()] | |
/* これ、ふつーにいけると思いきや。。。アウト!! | |
まぁでも、そらそうか。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Animal { | |
var f: (() -> Void)! | |
func bark() { | |
// インスタンスをキャプチャして、わざとメモリリークさせる | |
print(self) | |
} | |
deinit { print("deinit!!") } | |
} |