Shorthand Argument Names $0 : 只用於指代Closer聲明中的形參
Shorthand Argument Names
Swift automatically provides shorthand argument names to inline closures, which can be used to refer to the values of the closure’s arguments by the names $0
, $1
, $2
, and so on.
If you use these shorthand argument names within your closure expression, you can omit the closure’s argument list from its definition, and the number and type of the shorthand argument names will be inferred from the expected function type. The in
- reversedNames = names.sorted(by: { $0 > $1 } )
Here, $0
and $1
refer to the closure’s first and second String
arguments.
https://docs.swift.org/swift-book/LanguageGuide/Closures.html#ID97
public func map<U>(_ transform: (Value) -> U) -> Result<U, Error> {
return flatMap { .success(transform($0)) }
}
指代flatMap<U>(_ transform: (Value) -> Result<U, Error>) -> Result<U, Error>中的(Value)
/// Returns the result of applying `transform` to `Success`es’ values, or re-wrapping `Failure`’s errors.
public func flatMap<U>(_ transform: (Value) -> Result<U, Error>) -> Result<U, Error> {
return analysis(
ifSuccess: transform,
ifFailure: Result<U, Error>.failure)
}
Shorthand Argument Names $0 : 只用於指代Closer聲明中的形參