<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div dir="auto" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">Hey, all. I’m here to propose predefining a macro __swift__ when C code is compiled with Swift. We’ve gotten a few requests for it in the past and haven’t done it so that people don’t write header files that arbitrarily restrict features when used from Swift, or check for "Swift" when they really should be checking for modules support, or Objective-C mode, or nullability support. (Or worse, they guard code under __swift__ and then don’t ever test it, leading to failures to import the module from Swift.)</div><div class=""><br class=""></div><div class="">However, with Swift 3, it’s now become important for Objective-C authors to be able to control how their APIs look in modern Swift 3 without disrupting existing clients on Swift 2.3. (Or just because Swift 3 style looks out-of-place in Swift 2.3.) The most obvious way to do this would be to define a macro that has the Swift version in it. For Swift version X.Y.Z, we could use something like</div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class="">-D__swift__=XYYZZ</div></blockquote><div class=""><br class=""></div><div class="">e.g.</div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class="">-D__swift__=30001</div></blockquote><div class=""><br class=""></div>for Swift 3.0.1.<div class=""><br class=""></div><div class="">This is option (1). Option (2) would be to define __swift__ as a predicate that took version numbers:</div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class="">-D__swift__(MAJOR, MINOR, PATCH)=(MAJOR * 10000 + MINOR * 100 + PATCH &lt;= XYYZZ)</div><div class="">-D__swift__(MAJOR, MINOR, PATCH)=(MAJOR * 10000 + MINOR * 100 + PATCH&nbsp;&lt;= 30001)</div></blockquote><div class=""><br class=""></div>For completeness’ sake, option (3) would be to define a single macro __SWIFT3__, defined to 1. Clients can test for the presence or absence of this macro. I don’t like this option because (a) Swift 4 will also need to define __SWIFT3__, because it shouldn’t have the “old” behavior, and (b) we might very well want something like this again in the future; will we also define __SWIFT4__?<br class=""><div class=""><div class=""><div class=""><br class=""></div>(Why __swift__ and not __SWIFT__? By analogy with __cplusplus [sic] and __clang__, although not __STDC__ or&nbsp;<br class="">__STDC_VERSION__. I don’t actually have a strong opinion here.)</div></div><div class=""><br class=""></div><div class="">Thoughts?</div><div class="">Jordan</div><div class=""><br class=""></div><div class="">P.S. For Apple folks, this is&nbsp;<a href="rdar://problem/26921435" class="">rdar://problem/26921435</a>.</div></div></body></html>