Skip to content

Instantly share code, notes, and snippets.

@sammoore
Last active August 18, 2016 01:24
Show Gist options
  • Save sammoore/727a534b610417de81a37189cd27cb5d to your computer and use it in GitHub Desktop.
Save sammoore/727a534b610417de81a37189cd27cb5d to your computer and use it in GitHub Desktop.
Tuples in Swift can usually be directly used as parameters to a function or initializer. It seems that when the parameters to the given function are generics, the compiler can no longer coerce the tuple as parameters. Tested with Swift 2.2 / Xcode 7.3.
// Tested using Swift 2.2 / Xcode 7.3
import Swift
// A function and initializer with 2 parameters of explicit types.
func stringTuple(_ lhs: String, _ rhs: String) {
print("\(lhs), \(rhs)")
}
struct StringTuple {
init(_ lhs: String, _ rhs: String) {
print("\(lhs), \(rhs)")
}
}
// A similar function and initializer, with 2 parameters of the same generic type.
func genericTuple<T>(_ lhs: T, _ rhs: T) {
print("\(lhs), \(rhs)")
}
struct GenericTuple<T> {
init (_ lhs: T, _ rhs: T) {
print("\(lhs), \(rhs)")
}
}
// Test calling all of the above with a tuple.
let tuple = ("Hello", "World!")
// Both work, as expected.
stringTuple(tuple)
let stringPair = StringTuple(tuple)
// Error: "Missing argument for parameter #2 in call"
genericTuple(tuple)
let genericPair = GenericTuple(tuple)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment