Last active
January 25, 2018 10:50
-
-
Save Kuniwak/6f0997054df77739ad150c8582d37f91 to your computer and use it in GitHub Desktop.
SE-0176 の理解を確認するための例題集です
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
// 問題: コンパイルエラーもしくは実行時警告になる testX 関数をすべて答えてください。 | |
import XCTest | |
class SE0176PlaygroundTests: XCTestCase { | |
func test1() { | |
struct ExampleStruct { | |
mutating func assignedBy(_ block: () -> ExampleStruct) { | |
self = block() | |
} | |
} | |
var x = ExampleStruct() | |
x.assignedBy { ExampleStruct() } | |
} | |
func test2() { | |
struct ExampleStruct { | |
mutating func assignedBy(_ block: () -> ExampleStruct) { | |
self = block() | |
} | |
} | |
var x = ExampleStruct() | |
x.assignedBy { x } | |
} | |
func test3() { | |
struct ExampleStruct { | |
mutating func assignedBy(_ block: (inout ExampleStruct) -> Void) { | |
block(&self) | |
} | |
} | |
var x = ExampleStruct() | |
x.assignedBy { y in y = x } | |
} | |
func test4() { | |
struct ExampleStruct { | |
var property: String? | |
mutating func assignedBy(_ block: () -> String?) { | |
self.property = block() | |
} | |
} | |
var x = ExampleStruct() | |
x.assignedBy { "Example" } | |
} | |
func test5() { | |
struct ExampleStruct { | |
var property: String? | |
mutating func assignedBy(_ block: () -> String?) { | |
self.property = block() | |
} | |
} | |
var x = ExampleStruct() | |
x.assignedBy { x.property } | |
} | |
func test6() { | |
var x = 0 | |
x = { x + 1 }() | |
} | |
func test7() { | |
class ExampleClass { | |
var property: ExampleClass? | |
func assignedBy(_ block: () -> ExampleClass?) { | |
self.property = block() | |
} | |
} | |
let x = ExampleClass() | |
x.assignedBy { x } | |
} | |
func test8() { | |
class ExampleClass { | |
var property: ExampleClass? | |
func assignedBy(_ block: (inout ExampleClass?) -> Void) { | |
block(&self.property) | |
} | |
} | |
let x = ExampleClass() | |
x.assignedBy { y in y = x } | |
} | |
func test9() { | |
var global = 0 | |
func swapGlobal(with another: Int) { | |
global = another | |
} | |
swapGlobal(with: global) | |
} | |
func test10() { | |
var global = 0 | |
func assignGlobal(to another: inout Int) { | |
another = global | |
} | |
assignGlobal(to: &global) | |
} | |
func test11() { | |
var global = 0 | |
func swapGlobal(with another: inout Int) { | |
global = another | |
} | |
swapGlobal(with: &global) | |
} | |
func test12() { | |
class ExampleClass { | |
var property: Int? | |
func doSomething(_ x: UnsafeMutableRawPointer) { | |
print(x) | |
} | |
} | |
var x = ExampleClass() | |
x.doSomething(&x) | |
} | |
func test13() { | |
class ExampleClass { | |
var property: Int? | |
func doSomething(_ x: UnsafeMutableRawPointer) { | |
print(x == &self.property) | |
} | |
} | |
var x = ExampleClass() | |
x.doSomething(&x.property) | |
} | |
} | |
// 答えは https://gist.github.com/Kuniwak/a32883a62a37bf72f351fa3dc9c5aac7 で確認! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment