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 i: Character? | |
test("Optional Binding") { | |
for _ in 0...repeatCount { | |
if let us = s { | |
i = us.first | |
} else { | |
i = "i".first | |
} | |
} | |
} |
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
test("Optional Binding") { | |
for _ in 0...repeatCount { | |
let unrapped: String | |
if let tmp = s { | |
unrapped = tmp | |
} else { | |
unrapped = "default" | |
} | |
} | |
} |
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
func runSpeedTests() { | |
let s: String? = "test" | |
let repeatCount = 10000000000 | |
var i = 0 | |
test("Optional Binding") { | |
for _ in 0...repeatCount { | |
if let _ = s { i += 1 } | |
} | |
} |
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
func runSpeedTests() { | |
let s: String? = "test" | |
let repeatCount = 100000000 | |
test("Optional Binding") { | |
for _ in 0...repeatCount { | |
if let _ = s {} | |
} | |
} |
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
struct AnyCodingKeys: CodingKey { | |
var stringValue: String | |
var intValue: Int? | |
init?(stringValue: String) { self.stringValue = stringValue } | |
init?(intValue: Int) { | |
self.stringValue = String(intValue) | |
self.intValue = intValue | |
} |
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
struct Permission<Holder: PermissionHolder> { | |
let holder: Holder | |
} | |
protocol PermissionHolder { | |
var can: Permission<Self> { get } | |
} | |
extension PermissionHolder { | |
var can: Permission<Self> { return Permission(holder: self) } |
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 json = """ | |
{ | |
"id": 1, | |
"url":"", | |
"size":{ "width": 100, "height": 50 } | |
} | |
""".data(using: .utf8)! | |
protocol MyCodable: Codable { | |
static func decode(from decoder: Decoder) throws -> Self |
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
func const<A, B>(_ value: A) -> (B) -> A { | |
return { _ in value } | |
} | |
let f_const: (String) -> () = { const(())(foo("bar")($0)) } | |
f_const("hoge") // 返値がVoidなのでWarningが発生しない |
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
func foo(_ string: String) -> (String) -> String { | |
return { s in | |
let result = string + " " + s | |
print(result) | |
return result | |
} | |
} | |
let f: (String) -> String = foo("bar") | |
f("baz") // result of call is unused が発生 |
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
func operation(_ sub1: () -> (), _ sub2: () -> ()) { | |
sub1() | |
sub2() | |
} |