[swift-evolution] Compiler directive for current closure reference

Jordan Rose jordan_rose at apple.com
Mon Feb 22 12:41:27 CST 2016


Closures don't have identity (i.e. you can't use === on them). ObjC blocks do, so if you really need it you can pass things around as @convention(objc_block), but closures don't. So I think you'd have to start there if you want to do anything like this.

Jordan




> On Feb 22, 2016, at 10:20, Taras Zakharko via swift-evolution <swift-evolution at swift.org> wrote:
> 
> Dear all, 
> 
> I apologise in advance if this has already been suggested (or maybe even implemented), but its very difficult to keep track of the swift evolution list :) 
> 
> We already have compiler directives like #line, #function. I would like to suggest a new directive #closure (name preliminary) that expands to the reference to the closure, function or method in whose body the directive occurs. Motivation: this will allow one to conveniently use design patterns like these:
> 
> addHandler({
> 	guard stillRelevant() else {
>          removeHandler(#closure)
> 	}
> 
>       doSomething()
> })
> 
> 
> I am sure there are also other cases where such directive would come in handy. 
> 
> 
> 
> 
> 
> 
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution



More information about the swift-evolution mailing list