4

With Swift v4.2 they introduced the extension Bool.toggle(), I had this extension since earlier and now when I compile with Xcode10 it says Ambiguous use of 'toggle()'. I tried to make it ignore my own extension if Swift version is 4.2 or higher so I used the build configuration swift(>=) which should check if the current swift version is equal or higher than the specified version.

public extension Bool {

    #if swift(>=4.2)
    // DO NOTHING
    #else
    mutating func toggle() {
        self = !self
    }
    #endif
}

This should only see the comment DO NOTHING when on Xcode10 and on Xcode9 it should see the extended method. But whats happening is when on Xcode10 it still sees the method and I still get the error "Ambiguous use" and on Xcode9 it sees the method as well. I also changed the Build Setting Swift Language Version to Swift 4.2 and still the compiler sees the method.

Arbitur
  • 38,684
  • 22
  • 91
  • 128
  • 1
    That is strange. It *seems* that `#if swift(>=4.2)` evaluates to false when compiling in Xcode 10 beta3. – Martin R Jul 12 '18 at 15:11
  • On Version 10.0 beta (10L176w) it seems fine, I don't have beta3. As @MartinR has pointed out might be a bug, you could file a bug. – user1046037 Jul 12 '18 at 15:17
  • Yes I am on Beta3 – Arbitur Jul 12 '18 at 15:56
  • Related: [What is Swift 4.1.50?](https://stackoverflow.com/q/51429536/3939277) – Ky - Jul 19 '18 at 18:34
  • I cannot reproduce the "ambiguous use" error on Xcode 10 beta 3, but the problem you're facing is almost certainly due to running a Swift 4.2 compiler in Swift 4 compatibility mode, which gives you a language version of 4.1.50 – the language version check `#if swift(>=4.2)` is not sufficient to check for the *compiler* (and therefore stdlib) version. You would want something like `#if swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0))` (as shown in https://github.com/apple/swift-evolution/blob/master/proposals/0212-compiler-version-directive.md). Does this fix the issue? – Hamish Jul 19 '18 at 20:44

0 Answers0