[swift-evolution] [Pitch] #if swift version with third version component

Richard Wei rxrwei at gmail.com
Sat Oct 22 16:44:37 CDT 2016


Hi,

Foundation in 3.0-RELEASE didn't fully resolve the renaming in SE-0086, so we have `RegularExpression` on Linux and `NSRegularExpression` on macOS. The naming is now unified in Swift 3.0.1, but there doesn’t seem to be a possible way to resolve the code breaking change. Consider the example below:

Currently the following code is only compatible with 3.0-RELEASE, because RegularExpression on Linux becomes NSRegularExpression in 3.0.1, while we cannot use `#if` to differentiate between 3.0 and 3.0.1.

  #if !os(macOS)
  let regex = try RegularExpression(pattern: pattern, options: [ .dotMatchesLineSeparators ])
  #else
  let regex = try NSRegularExpression(pattern: pattern, options: [ .dotMatchesLineSeparators ])
  #endif

Proposed solution:

If `#if swift(>=)` can take the third version component, we can make the code compatible with 3.0.1 by the following:

  #if os(macOS) || swift(>=3.0.1)
  let regex = try NSRegularExpression(pattern: pattern, options: [ .dotMatchesLineSeparators ])  
  #else
  let regex = try RegularExpression(pattern: pattern, options: [ .dotMatchesLineSeparators ])
  #endif

Additionally, we can consider supporting the == operator, so that checking only the version with inconsistent naming is sufficient:

  #if !os(macOS) && swift(==3.0.0)
  let regex = try RegularExpression(pattern: pattern, options: [ .dotMatchesLineSeparators ])
  #else
  let regex = try NSRegularExpression(pattern: pattern, options: [ .dotMatchesLineSeparators ])
  #endif

Impact on existing code:

`#if swift(>=X.X.X)` and `#if swift(==X.X.X)` are purely additive.

-Richard

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20161022/db8068b4/attachment.html>


More information about the swift-evolution mailing list