Last active
August 18, 2016 01:24
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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