最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Swift: Closure type-definition with surronding parenthesis - Stack Overflow

programmeradmin2浏览0评论

Why is the type-definition packed in parentheses? Can someone explain how this syntax has to be read?

// Definition & initialization
var removal: (() -> Void)? = nil
// Invocation of the closure
removal?()

Why is the type-definition packed in parentheses? Can someone explain how this syntax has to be read?

// Definition & initialization
var removal: (() -> Void)? = nil
// Invocation of the closure
removal?()
Share Improve this question asked Mar 15 at 6:56 cluster1cluster1 5,8367 gold badges38 silver badges59 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

(() -> Void)? is an optional () -> Void in the same way that Int? is an optional Int. You can also write this as Optional<() -> Void>.

() -> Void is wrapped in parentheses because () -> Void? would mean a parameterless closure that returns an optional Void (i.e. () -> Optional<Void>).

Basically, -> has a lower "precedence" than ?.


In fact any type can be wrapped in parentheses can it will not change the meaning of that type. You can totally write (Int) instead of Int if you want. This is similar to how any expression can be wrapped in parentheses and it will not change the meaning of the expression.

发布评论

评论列表(0)

  1. 暂无评论