Friday, January 30, 2015

Swift: No Macros, No Compromises

Using Swift with Cocoa and Objective-C (via Andrew Bancroft, emphasis added):

Complex macros are used in C and Objective-C but have no counterpart in Swift. Complex macros are macros that do not define constants, including parenthesized, function-like macros. You use complex macros in C and Objective-C to avoid type-checking constraints or to avoid retyping large amounts of boilerplate code. However, macros can make debugging and refactoring difficult. In Swift, you can use functions and generics to achieve the same results without any compromises. Therefore, the complex macros that are in C and Objective-C source files are not made available to your Swift code.

This is the sort of thing that really bothers me about Apple and Swift. I think Apple oversold how ready it was this summer, but it’s understandable that the compiler is buggy and that debugging and performance are problematic. And there is certainly a case to be made (though I would disagree) that macros should not be in the language or should not be used in most cases. But the emphasized sentence is so laughably, obviously false that one wonders why Apple bothered to include it. The results are not the same, and there most certainly are compromises. There’s a lot that you just can’t do in Swift because it lacks macros. Do Swift’s designers really believe this? Does the author not know what he or she is talking about? Or is Apple being deliberately misleading in order to “sell” the language? Either way, this sort of writing does not belong in technical documentation, and it’s especially jarring considering how Microsoft has used those same words.

Update (2015-01-31): Christoffer Lernö:

Given how many warts we could’ve fixed in Swift with even rudimentary macros, the statement in the docs become even more ludicrous.

3 Comments RSS · Twitter

Apple are clever enough to realise that C style macros are problematic, but stupid enough not to realise there is something called hygienic macros in languages like Scheme.

[…] and __LINE__. Swift does not have a way of capturing the expression as a string, among other limitations from not having […]

[…] the early bluster about not needing macros, a real hygienic macro system has not been ruled […]

Leave a Comment