Reputation: 304
everyone. When I read the Closures, there is not a definition for inline closures.
Q1: How to understand inline in "inline closure"?
Q2: What is the different between "inline closure" and normal closure?
Thanks in advance for your help!
Upvotes: 7
Views: 3188
Reputation: 63272
An inline value is a value that's used directly, without first being assigned to an intermediate variable. Consider these two examples:
let number = 1
print(number)
Here, 1
is assigned to an intermediate variable, number
, which is then printed.
print(1)
Here, 1
, is an inlined integer literal, which is printed directly.
The same applies to closures.
let evenNumberFilter: (Int) -> Bool = { $0 % 2 == 0 }
print((0...10).filter(evenNumberFilter))
Here, { $0 % 2 == 0 }
is a closure (of type (Int) -> Bool
) that's assigned to the intermediate variable evenNumberFilter
before being used.
print((0...10).filter{ $0 % 2 == 0 })
In this case, { $0 % 2 == 0 }
was used directly. It's an inline closure.
Upvotes: 15