Allow $0: <type> in closures

  1. You write a closure and type inference lets you omit the types:
    ... { <stuff using $0> }

  2. Something changes in the stuff and you need to explicitly write the types:
    ... { ($0: T <some type>) in: <stuff using $0> }

  3. But now it doesn't compile; you have to replace all uses of $0 with some name.

Wouldn't it be nice if you didn't? I propose to make the code in #2 legal, and the obvious generalizations, such as $1.

2 Likes

Do you have some examples of when you'd need to explicitly write the type for $0?

I don’t think it’s necessary to designate variable type name in the closure, since it should always be declared explicitly in the type name of the closure itself. If the declared type doesn’t match what you expect, what you need is guard-let or try-catch-throw.

Also, in my opinion, the dollar variable should only be used when it has a very simple and clear reference. If it’s hard to understand, you should give it a name.

2 Likes

I would like that. If you chain a couple of short maps, this can confuse the compiler despite being totally reasonable for the human reader.

Terms of Service

Privacy Policy

Cookie Policy