Skip to content

Instantly share code, notes, and snippets.

@lorentey
Last active June 2, 2016 22:05
Show Gist options
  • Save lorentey/4dd95966e92e72534cce29966747e2a7 to your computer and use it in GitHub Desktop.
Save lorentey/4dd95966e92e72534cce29966747e2a7 to your computer and use it in GitHub Desktop.
The following snippet compiles under Swift 2.2, but not Swift 3. It defines a (quite useless) left shift operator on `Double` values. For some reason, Swift 3's type inference engine can go awry when such a shift operator is used in a subexpression.
public func <<(num: Double, exp: Int) -> Double {
return num * Double(1 << exp)
}
let s = (0.5 << 5) - 1
print(s)
@lorentey
Copy link
Author

lorentey commented Jun 2, 2016

Swift 2.2:

15.0

Swift 3:

inference-snafu.swift:5:20: error: binary operator '-' cannot be applied to operands of type 'Double' and 'Int'
let s = (0.5 << 5) - 1
        ~~~~~~~~~~ ^ ~
inference-snafu.swift:5:20: note: overloads for '-' exist with these partially matching parameter lists: (Int, Int), (Double, Double), (UnsafeMutablePointer<Pointee>, Int), (UnsafePointer<Pointee>, Int)
let s = (0.5 << 5) - 1
                   ^

@lorentey
Copy link
Author

lorentey commented Jun 2, 2016

@erica
Copy link

erica commented Jun 2, 2016

Are you sure that's a bug? It looks to me as if a bug were fixed not introduced

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment