If I understand correctly, Swift can determine the actual type of generics in various ways, including matching by return type. The same (or similar) mechanism is used to eliminate ambiguous overloaded functions. Thus, this works as expected:
func getValue<T>()->T? { return nil } func getValue()->Int? { return 13 } let b: Int? = getValue()
When doing this, b will be 13 . Technically, both function signatures are suitable, but the latter is more specific to the requested return type.
Add a second function and pass it through the tunnel:
func getGetValue<T>()->T? { return getValue() } let c: Int? = getGetValue()
When doing this, c will be nil . In fact, the compiler will choose the general implementation of getValue (), which will be called from getGetValue (), which I don't want. IMHO, the requested return value type must propagate over the second common one when choosing between two implementations of getValue (), which leads to the same behavior as in the first example.
What am I missing? (Xcode 7.1)
source share