[swift-evolution] [Pitch] Improving capturing semantics of local functions

Chris Lattner clattner at nondot.org
Mon Nov 20 20:59:21 CST 2017


> On Nov 12, 2017, at 8:11 PM, Brent Royal-Gordon via swift-evolution <swift-evolution at swift.org> wrote:
> 
>> 
>> Secondly, this proposal suggests allowing the same capture list syntax from closures in local functions. Capture lists would still be invalid in top-level and member functions.
> 
> 
> I think this is a good idea, but I don't like bringing the already weird use of `in` to actual functions.
> 
> By analogy with the current closure syntax, the capture list ought to go somewhere before the parameter list, in one of these slots:
> 
> 1.	func fn<T>[foo, bar](param: T) throws -> T where T: Equatable { … }
> 2.	func fn[foo, bar]<T>(param: T) throws -> T where T: Equatable { … }
> 3.	func [foo, bar] fn<T>(param: T) throws -> T where T: Equatable { … }
> 4.	[foo, bar] func fn<T>(param: T) throws -> T where T: Equatable { … }
> 
> Of these options, I actually think #4 reads best; 1 and 2 are very cluttered, and 3 just seems weird. But it seems like the one that would be easiest to misparse.

This is relatively rare, so I’d suggest introducing a context sensitive keyword to make it explicit, perhaps:

5. func fn<T>[foo, bar](param: T) throws -> T where T: Equatable captures [foo, bar] { … }

It makes sense (IMO) to keep it near the body of the function, since it is more an artifact of the implementation than it is about the API.  Yes I know that caring about the API of a local function is weird :-)  


-Chris

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20171120/8edf5850/attachment.html>


More information about the swift-evolution mailing list