首页 > 其他 > 详细

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

时间:2018-10-10 13:27:24      阅读:168      评论:0      收藏:0      [点我收藏+]

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!