Sunday, August 24, 2014

Macro-like Syntax Extensions Through Pseudo-closures

Christoffer Lernö:

For normal closures there is no way to fix this problem. We can only have the closure return to it’s caller, which in this case is the function times we added to Int.

In many languages with macros, we would construct syntax by directly working with the syntax tree created by the compiler. This is can get fairly advanced and isn’t very easy to read.

As we see in this example we could do a lot of nice things if we just had something which “looks like a closure but unwinds the stack like an exception”

I would really like to see something like this in Swift.

1 Comment RSS · Twitter

That reminds me why I prefer my MFIndexSetForeach macro to -[NSIndexSet enumerateIndexesUsingBlock:]. The macro is really ugly, but it actually does what you expect a loop would do, break and continue work (goto works too!) but most importantly return does what you expect it to do. Given how easy it is to misread "return" in the trailing closure syntax to something that return from the function in the outer scope, I wonder if it should be allowed at all.

Leave a Comment