[swift-dev] RFC: internal language versioning

David Zarzycki zarzycki at icloud.com
Sat Aug 19 07:11:47 CDT 2017


Hi Ewa!

In commit 93786d8e264d095256169a6a2552970ea785036f, you wrote: “Removing isSwiftVersion4, isSwiftVersion5.  We’ll need to come up with a better way to conditionalize code based on language version.”

This is certainly true! I wondered what would happen as new versions came along. :-)

It seems to me like the right approach is this:

    bool isSwiftVersionLessThan(unsigned major, unsigned minor = 0);
    // hopefully we’ll never need ’minor’ level precession

With such an API, bug fixes, version specific behavior, and future features are easy to implement:

// Bug fixed in version 4
if (isSwiftVersionLessThan(4)) {
  // old way
} else {
  // new way
}

// Version 7 feature work
if (isSwiftVersionLessThan(7)) {
  // pre feature logic
} else {
  // new feature work logic
}

// Broken behavior specific to version 3 and 4, but not version 1, 2, and 5+
if (isSwiftVersionLessThan(5) && !isSwiftVersionLessThan(3)) {
  // Swift 3 and 4 code
} else {
  // normal path
}

Alternatively, the API could be “isSwiftVersionGreaterThanOrEqualTo()” but that seems like a mouthful. Similarly, the API could be “isSwiftVersionInHalfOpenRange()”, but that seems awkward because the future is hopefully endless.

What to people think?

Dave
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-dev/attachments/20170819/044cadb3/attachment.html>


More information about the swift-dev mailing list