Created
July 1, 2022 08:52
-
-
Save lvalenta/c557cda96123e838baeb65f22bca83b4 to your computer and use it in GitHub Desktop.
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 orderFood<T: Server>(server: T, size: Int) { | |
prepareFood(server: server, size: size) | |
if !isFoodReady(server: server) { | |
fatalError("Food is not ready for some reason") | |
} | |
} |
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 isFoodReady(server: Server) -> Bool { | |
server.isFoodReady() | |
} |
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
output.isFoodReady<A where A: output.Server>(server: A) -> Swift.Bool: //generic function | |
push rbp | |
mov rbp, rsp | |
push r13 | |
sub rsp, 40 | |
mov qword ptr [rbp - 40], rdx | |
mov rax, rsi | |
mov rsi, qword ptr [rbp - 40] | |
mov qword ptr [rbp - 32], rax | |
mov r13, rdi | |
mov rdi, qword ptr [rbp - 32] | |
mov qword ptr [rbp - 24], 0 | |
mov qword ptr [rbp - 16], rdi | |
mov qword ptr [rbp - 24], r13 | |
mov rax, qword ptr [rsi + 8] | |
call rax | |
add rsp, 40 | |
pop r13 | |
pop rbp | |
ret | |
output.isFoodReady(server: output.Server) -> Swift.Bool: //non-generic function | |
push rbp | |
mov rbp, rsp | |
push r13 | |
sub rsp, 24 | |
mov qword ptr [rbp - 16], 0 | |
mov qword ptr [rbp - 16], rdi | |
mov rsi, qword ptr [rdi + 24] | |
mov qword ptr [rbp - 32], rsi | |
mov rax, qword ptr [rdi + 32] | |
mov qword ptr [rbp - 24], rax | |
call __swift_project_boxed_opaque_existential_1 | |
mov rdi, qword ptr [rbp - 32] | |
mov rsi, qword ptr [rbp - 24] | |
mov r13, rax | |
mov rax, qword ptr [rsi + 8] | |
call rax | |
add rsp, 24 | |
pop r13 | |
pop rbp | |
ret |
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
lea rsi, [rip + (full type metadata for output.DinnerServer)] | |
add rsi, 8 | |
lea rdx, [rip + (protocol witness table for output.DinnerServer : output.Server in output)] | |
call (output.isFoodReady<A where A: output.Server>(server: A) -> Swift.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
lea rax, [rip + (full type metadata for output.DinnerServer)] | |
add rax, 8 | |
mov qword ptr [rbp - 16], rax | |
lea rax, [rip + (protocol witness table for output.DinnerServer : output.Server in output)] | |
mov qword ptr [rbp - 8], rax | |
lea rdi, [rbp - 40] | |
call (output.isFoodReady(server: output.Server) -> Swift.Bool) | |
lea rdi, [rbp - 40] | |
call __swift_destroy_boxed_opaque_existential_1 | |
xor eax, eax | |
add rsp, 48 | |
pop rbp | |
ret |
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 isFoodReady(server: any Server) -> Bool { | |
server.isFoodReady() | |
} |
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 measure(functions: [MeasureFunc], repeats: 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
func oneRunOf(_ function: () -> ()) -> Double { | |
let timeBefore = CACurrentMediaTime() | |
function() | |
return CACurrentMediaTime() - timeBefore | |
} |
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
final class TestingClass: Testable { | |
let variable1 = UUID() | |
let variable2 = UUID() | |
let variable3 = UUID() | |
let variable4 = UUID() | |
let variable5 = UUID() | |
} |
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 getAppendingToArray<T: Testable>(_ item: T) { | |
var array: Array<T> = [] | |
for _ in 0..<100000 { | |
array.append(item) | |
} | |
} |
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 getAppendingToArrayDynamicDispatch(_ item: Testable) { | |
var array: Array<Testable> = [] | |
for _ in 0..<100000 { | |
array.append(item) | |
} | |
} |
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 getAppendingAndCreatingArray<T: Testable>(ofType usedType: T.Type) { | |
var array: Array<T> = [] | |
for _ in 0..<100000 { | |
array.append(usedType.init()) | |
} | |
} |
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 getAppendingAndCreatingArrayDynamicDispatch(ofType usedType: Testable.Type) { | |
var array: Array<Testable> = [] | |
for _ in 0..<100000 { | |
array.append(usedType.init()) | |
} | |
} |
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
final class RecursionStaticDispatch<T: Testable> { | |
let storage: T | |
let height: Int | |
init(item: T, height: Int) { | |
self.storage = item | |
self.height = height | |
recurseIfRecursionLessThanFive() | |
} | |
func recurseIfRecursionLessThanFive() { | |
if height < 5 { | |
let _ = RecursionStaticDispatch(item: storage, height: height + 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
final class RecursionDynamicDispatch { | |
let storage: Testable | |
let height: Int | |
init(item: Testable, height: Int) { | |
self.storage = item | |
self.height = height | |
recurseIfRecursionLessThanFive() | |
} | |
func recurseIfRecursionLessThanFive() { | |
if height < 5 { | |
let _ = RecursionDynamicDispatch(item: storage, height: height + 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
final class ExampleCoordinator { | |
typealias Dependencies = AllDependencies | |
(...) | |
} |
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
final class ExampleCoordinator<Dependencies: AllDependencies> { | |
(...) | |
} |
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 Developer { } |
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 CleevioDeveloper: Developer { } |
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 invoiceForCustomer(developer: any Developer) { } | |
let developer = CleevioDeveloper() | |
invoiceForCustomer(developer: developer) |
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 invoiceForCustomer<T: Developer>(developer: T) { } | |
let developer: any Developer = CleevioDeveloper() | |
invoiceForCustomer(developer: developer) |
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 CanFly { } | |
final class ViewModel<T: CanFly> { (…) } | |
final class ViewController<T: CanFly>: UIViewController { | |
let viewModel: ViewModel<T> | |
(…) | |
} |
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 isFoodReady(server: Server) -> Bool { | |
server.isFoodReady() | |
} |
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 isFoodReady<T: Server>(server: T) { | |
server.isFoodReady() | |
} |
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 isFoodReady(server: DinnerServer) { | |
server.isFoodReady() | |
} |
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 Server { | |
func isFoodReady() -> Bool | |
func preparedIngredients(size: Int) -> () | |
func finalizePreparation() -> () | |
} |
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
extension DinnerServer: Server { | |
func server.prepareIngredients(size: Int) { | |
var preparedIngredients = 0 | |
while preparedIngredients < size { | |
preparedIngredients += 1 | |
} | |
} | |
func finalizePreparation() { } | |
} |
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 prepareFood<T: Server>(server: T, size: Int) { | |
server.prepareIngredients(size: size) | |
server.finalizePreparation() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment