Closures and trailing closure syntax
WHAT IT TESTS: first-class functions and capture. OUTLINE: closures are self-contained function blocks that capture surrounding state; a final closure parameter can be passed as a trailing closure outside the parentheses.
WHAT IT TESTS: fluency with Swift's first-class function blocks. ANSWER OUTLINE: a closure is a self-contained block of functionality that can be stored, passed, and that captures references to constants and variables from its defining context. When a function's last parameter is a closure, you can write it as a trailing closure after the closing parenthesis, and if it is the only argument you can drop the parentheses entirely. RED FLAG: conflating trailing closure syntax with the separate concepts of escaping or autoclosure.
Read the original → interview
- #swift
- #closures
- #functions
- #trailing-closure
- #syntax
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.