码迷,mamicode.com
首页 > 其他好文 > 详细

Shorthand Argument Names $0 : 只用于指代Closer声明中的形参

时间:2018-10-10 14:15:33      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:mat   The   str   tran   ati   closure   lin   mit   flat   

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 keyword can also be omitted, because the closure expression is made up entirely of its body:

  1. 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声明中的形参

标签:mat   The   str   tran   ati   closure   lin   mit   flat   

原文地址:https://www.cnblogs.com/feng9exe/p/9765515.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!