Why does swift use an inconsistent syntax in ifdefs?

Why does swift use:
#if !swift(<=4.2)

and not

#if !(swift<=4.2)

(Matching the rest of the language)?

1 Like

"swift" isn't a value here; it's the name of the "function" that takes an expected version and compares to it. You can argue we should have spelled it swift(atLeast: 4.2) or swiftVersion(atLeast: 4.2) instead of having magic syntax swift(>=4.2), and I probably would agree with you in retrospect, but you're still supposed to think of it like a function.

Next question: why is it a function at all? Because so far we've been saving plain names for "flags provided on the command line with -D". It simplifies the compiler implementation to not have different "types" of identifiers that can appear in #if expressions. (We may end up there eventually, but for now it seems to be working okay.)

EDIT: …and also this is a version, not a floating-point number, so we need to handle things like swift(>=4.2.1).

5 Likes
Terms of Service

Privacy Policy

Cookie Policy