1

Given

class MyClass {
    static let Anything = 1

    func wasCalled<T:AnyObject where T:Equatable>(values: [T]) { }
}

why does this compile:

MyClass().wasCalled([1, "a string"])

but this produces "cannot invoke 'wasCalled' with an argument list of type '(NSArray)'"

MyClass().wasCalled([MyClass.Anything, "a string"])

as does

let n = 2
MyClass().wasCalled([n, "a string"])

using: Xcode 7 beta 6, Swift 2

jwilkey
  • 326
  • 3
  • 11
  • `1` doesn't have type, but `Anything` and `n` have type of `Int` – Bryan Chen Sep 08 '15 at 05:28
  • 1
    Looks like a candidate for RADAR? – Shripada Sep 08 '15 at 05:35
  • would '1' not be inferred as type Int? Besides, Int still satisfies T expectations, so why can't the compiler resolve it as [T], not NSArray? – jwilkey Sep 08 '15 at 05:36
  • Because `MyClass.Anything` is an `Int`, whereas `"a string"` is a string. Hence, the typechecker is unable to deduce one unambiguous type for use with Swift's built-in array, and makes it a "one object fits all" `NSArray` instead (since it's inherently heterogeneous). – The Paramagnetic Croissant Sep 08 '15 at 05:52
  • that doesn't explain why `MyClass().wasCalled([1, "a string"])` compiles – jwilkey Sep 08 '15 at 14:46

0 Answers0