Inline Expressions and @autoclosure in Swift. Insanely Simple.

KD Knowledge Diet
3 min readFeb 27, 2023

Inline Expressions in Swift

In Swift, an inline expression is a code statement that is evaluated immediately when it is encountered in the program’s execution flow. The result of the expression is used at the location where the expression is written. Using inline expressions can help to reduce code verbosity and improve code readability.

One way to enable inline expressions in Swift is to use the @autoclosure attribute. This attribute allows you to define an argument that automatically gets wrapped in a closure, which can help improve performance and simplify code.

Using @autoclosure

The @autoclosure attribute can be used in a variety of ways. For example, you can use it to inline assignments or pass errors as expressions. Here are some examples:

func myFunction(_ closure: () -> Void) {
closure()
}

var myVariable = 0
myFunction { myVariable = 1 }
print(myVariable) // Output: 1
let dictionary = ["name": "John"]
let age = (dictionary["age"] as? Int) ?? 25
print(age) // Output: 25
func assert(_ expression: @autoclosure () -> Bool, _ message: String) {
if !expression() {
print(message)
}
}

assert(1 + 1 == 2, "Math is broken!") // Output: nothing
assert(1 + 1 == 3, "Math is still broken!") // Output: "Math is still broken!"

In the first example, we use an inline expression to modify the value of a variable (myVariable). In the second example, we use the ?? operator to provide a default value for a dictionary lookup. In the third example, we use an inline expression to check a mathematical expression and print a message if it fails.

Improving Code Readability

Using @autoclosure can also help to improve the readability of your code. Consider the following example:

UIView.animate(withDuration: 0.25) {
view.frame.origin.y = 100
}

In this example, we use an inline expression to animate a view’s frame. By using the @autoclosure attribute, we can simplify the code and make it easier to read.

Here’s another example that demonstrates how @autoclosure can be used to improve code readability:

extension Dictionary where Value == Any {
func value<T>(forKey key: Key, defaultValue: @autoclosure () -> T) -> T {
guard let value = self[key] as? T else {
return defaultValue()
}
return value
}
}

let dictionary = ["name": "John"]
let age = dictionary.value(forKey: "age", defaultValue: 25)
print(age) // Output: 25

In this example, we define an extension on Dictionary that provides a convenient way to look up a value for a key with a default value. By using the @autoclosure attribute, we can simplify the code and make it more readable.

Conclusion

Inline expressions are a powerful feature of Swift that can help you write cleaner, more expressive code. By using the @autoclosure attribute, you can simplify your code and improve its performance. Whether you're modifying a variable, animating a view, or looking up a value in a dictionary, inline expressions can help you write better code.

--

--

KD Knowledge Diet

Software Engineer, Mobile Developer living in Seoul. I hate people using difficult words. Why not using simple words? Keep It Simple Stupid!